Static
isCheck 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.
Static
InfinityThe infinity money object.
Static
ZeroThe zero money object.
Static
absStatic
addAdd 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")
Static
divideStatic
formatFormats a money object.
The money object to be formatted.
Optional
options: 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"
Static
fromConverts a number or string amount to a money object.
The amount.
The currency code.
Optional
options: { locale?: LocalesArgument }Optional parameters.
Optional
locale?: 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" }
Static
getReturns a given currency symbol
The currency code, e.g: "USD"
The options for getting the currency symbol.
Optional
locale?: LocalesArgumentThe locale to use for formatting
The currency symbol.
Static
maxGet 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")
Static
minGet 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")
Static
multiplyStatic
percentageCalculate the percentage of one Money object relative to another.
The calculated percentage as a string.
Static
subtractStatic
toConverts a money object to an amount.
The money object.
Optional
options: { locale?: LocalesArgument }Optional parameters.
Optional
locale?: LocalesArgumentThe locale to use for formatting.
The amount.
Static
toConverts 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"