StaticisCheck if a money amount is between a minimum and maximum amount (inclusive).
Money.is.between({ value: { cents: 12345, currency: "USD" }, min: { cents: 12345, currency: "USD" }, max: { cents: 54321, currency: "USD" } }); // true
Money.is.between({ value: { cents: 54321, currency: "USD" }, min: { cents: 12345, currency: "USD" }, max: { cents: 54321, currency: "USD" } }); // true
Money.is.between({ value: { cents: 12345, currency: "USD" }, min: { cents: 54321, currency: "USD" }, max: { cents: 54321, currency: "USD" } }); // false
Money.is.between({ value: { cents: 54321, currency: "USD" }, min: { cents: 12345, currency: "USD" }, max: { cents: 54321, currency: "GBP" } }); // Error("Cannot perform operations on money objects in different currencies")
Check if money1 is equal to money2.
Money.is.equal({ cents: 54321, currency: "USD" }, { cents: 54321, currency: "USD" }); // true
Money.is.equal({ cents: 54321, currency: "USD" }, { cents: 54322, currency: "USD" }); // false
Money.is.equal({ cents: 54321, currency: "USD" }, { cents: 54321, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
Check if money1 is greater than money2.
Money.is.greaterThan({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // true
Money.is.greaterThan({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "USD" }); // false
Money.is.greaterThan({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
Check if money1 is greater than or equal to money2.
Money.is.greaterThanOrEqual({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // true
Money.is.greaterThanOrEqual({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "USD" }); // false
Money.is.greaterThanOrEqual({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
Check if money1 is less than money2.
Money.is.lessThan({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // false
Money.is.lessThan({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "USD" }); // true
Money.is.lessThan({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
Check if money1 is less than or equal to money2.
Money.is.lessThanOrEqual({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // false
Money.is.lessThanOrEqual({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "USD" }); // true
Money.is.lessThanOrEqual({ cents: 12345, currency: "USD" }, { cents: 54321, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
Type guard to check if an object is a Money object.
Check if money1 and money2 are in the same currency.
Check if money is zero.
StaticzeroReturns a zero money object in the given currency.
The currency code.
A money object with 0 cents in the specified currency.
StaticInfinityThe infinity money object.
StaticZeroThe zero money object.
StaticabsStaticaddAdd two or more money objects.
The money objects.
The result of adding the money objects.
Money.add({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // { cents: 66666, currency: "USD" }
Money.add({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }, { cents: 11, currency: "USD" }); // { cents: 66677, currency: "USD" }
Money.add({ cents: 54321, currency: "GBP" }, { cents: 0, currency: "USD" }); // { cents: 54321, currency: "GBP" }
Money.add({ cents: 54321, currency: "GBP" }, { cents: 10, currency: "USD" }); // Error("Cannot perform operations on money objects in different currencies")
StaticdivideStaticformatFormats a money object.
The money object to be formatted.
Optionaloptions: MoneyFormatOptionsThe options for formatting.
The formatted currency.
Money.format({ cents: 123456, currency: "USD" }); // "$1,234.56 USD"
Money.format({ cents: 123456, currency: "GBP" }); // "£1,234.56 EUR"
Money.format({ cents: 123456, currency: "JPY" }); // "¥123,456 JPY"
Money.format({ cents: 123456, currency: "USD" }, { showCents: false }); // "$1,234 USD"
Money.format({ cents: 123456, currency: "USD" }, { showCurrency: false }); // "$1,234.56"
Money.format({ cents: 123456, currency: "EUR" }, { locale: "de-DE" }); // "1.234,56 € EUR"
Money.format({ cents: 123456, currency: "EUR" }, { locale: "de" }); // "1.234,56 € EUR"
Money.format({ cents: 150_000, currency: "EUR" }, { compact: true, locale: "de", showCents: false }); // "1.5k € EUR"
Money.format({ cents: 150_000, currency: "USD" }, { compact: true, showCents: false }); // "$1.5K USD"
StaticformatFormats a range of two money objects, showing the currency only once at the end. Useful for displaying min-max ranges without duplicating currency symbols.
The minimum money object.
The maximum money object.
Optionaloptions: MoneyFormatOptionsThe options for formatting.
The formatted currency range.
Money.formatRange({ cents: 50000, currency: "USD" }, { cents: 500000, currency: "USD" }, { compact: true }); // "$500 - $5K USD"
Money.formatRange({ cents: 50000, currency: "AED" }, { cents: 500000, currency: "AED" }, { compact: true }); // "500 - 5K AED"
Money.formatRange({ cents: 50000, currency: "USD" }, { cents: 50000, currency: "USD" }); // "$500.00 USD"
StaticformatFormats a money object with an equivalent amount in another currency shown in parentheses. If the two amounts share the same currency, or if equivalentMoney is null/undefined, only the primary amount is shown.
The primary money object to format.
The equivalent amount in another currency.
Optionaloptions: MoneyFormatOptionsThe options for formatting both amounts.
The formatted string.
Money.formatWithEquivalent({ cents: 100_00, currency: "CAD" }, { cents: 75_00, currency: "USD" }); // "$100.00 CAD ($75.00 USD)"
Money.formatWithEquivalent({ cents: 100_00, currency: "USD" }, { cents: 100_00, currency: "USD" }); // "$100.00 USD"
Money.formatWithEquivalent({ cents: 100_00, currency: "USD" }, null); // "$100.00 USD"
StaticfromConverts a number or string amount to a money object.
The amount.
The currency code.
Optionaloptions: { locale?: LocalesArgument }Optional parameters.
Optionallocale?: LocalesArgumentThe locale to use for formatting.
The money object.
Money.fromAmount(123.45, "USD"); // { cents: 12345, currency: "USD" }
Money.fromAmount("123.45", "USD"); // { cents: 12345, currency: "USD" }
Money.fromAmount(123.45, "CAD"); // { cents: 12345, currency: "CAD" }
Money.fromAmount(123.45, "USD", { locale: "en-US" }); // { cents: 12345, currency: "USD" }
StaticgetReturns a given currency symbol
The currency code, e.g: "USD"
The options for getting the currency symbol.
Optionallocale?: LocalesArgumentThe locale to use for formatting
The currency symbol.
StaticmaxGet the maximum of two or more money objects.
The money objects.
The maximum money object.
Money.max({ cents: 100, currency: "USD" }, { cents: 200, currency: "USD" }) // { cents: 200, currency: "USD" }
Money.max({ cents: 100, currency: "USD" }, { cents: 300, currency: "USD" }, { cents: 200, currency: "USD" }) // { cents: 300, currency: "USD" }
Money.max({ cents: 100, currency: "USD" }, { cents: 100, currency: "GBP" }) // Error("Cannot perform operations on money objects in different currencies")
StaticminGet the minimum of two or more money objects.
The money objects.
The minimum money object.
Money.min({ cents: 100, currency: "USD" }, { cents: 200, currency: "USD" }) // { cents: 100, currency: "USD" }
Money.min({ cents: 100, currency: "USD" }, { cents: 300, currency: "USD" }, { cents: 200, currency: "USD" }) // { cents: 200, currency: "USD" }
Money.min({ cents: 100, currency: "USD" }, { cents: 100, currency: "GBP" }) // Error("Cannot perform operations on money objects in different currencies")
StaticmultiplyStaticpercentageCalculate the percentage of one Money object relative to another.
The calculated percentage as a string.
StaticsubtractStatictoConverts a money object to an amount.
The money object.
Optionaloptions: { locale?: LocalesArgument }Optional parameters.
Optionallocale?: LocalesArgumentThe locale to use for formatting.
The amount.
StatictoConverts a money object to a string amount.
The money object.
The string amount.
Money is not intended to be an instantiable class. We are including these properties here to allow us to use "Money as a type"