@tremendous/help
    Preparing search index...

    Class Money

    Index

    Constructors

    Properties

    cents: number

    Money is not intended to be an instantiable class. We are including these properties here to allow us to use "Money as a type"

    currency: string

    Accessors

    • get is(): {
          between: (params: { max: Money; min: Money; value: Money }) => boolean;
          equal: (money1: Money, money2: Money) => boolean;
          greaterThan: (money1: Money, money2: Money) => boolean;
          greaterThanOrEqual: (money1: Money, money2: Money) => boolean;
          lessThan: (money1: Money, money2: Money) => boolean;
          lessThanOrEqual: (money1: Money, money2: Money) => boolean;
          Money: (obj: unknown) => boolean;
          sameCurrency: (money1: Money, money2: Money) => boolean;
          zero: (money?: null | Money) => boolean;
          get not(): {
              between: (params: { max: Money; min: Money; value: Money }) => boolean;
              equal: (money1: Money, money2: Money) => boolean;
              Money: (obj: unknown) => boolean;
              sameCurrency: (money1: Money, money2: Money) => boolean;
              zero: (money?: null | Money) => boolean;
          };
      }

      Returns {
          between: (params: { max: Money; min: Money; value: Money }) => boolean;
          equal: (money1: Money, money2: Money) => boolean;
          greaterThan: (money1: Money, money2: Money) => boolean;
          greaterThanOrEqual: (money1: Money, money2: Money) => boolean;
          lessThan: (money1: Money, money2: Money) => boolean;
          lessThanOrEqual: (money1: Money, money2: Money) => boolean;
          Money: (obj: unknown) => boolean;
          sameCurrency: (money1: Money, money2: Money) => boolean;
          zero: (money?: null | Money) => boolean;
          get not(): {
              between: (params: { max: Money; min: Money; value: Money }) => boolean;
              equal: (money1: Money, money2: Money) => boolean;
              Money: (obj: unknown) => boolean;
              sameCurrency: (money1: Money, money2: Money) => boolean;
              zero: (money?: null | Money) => boolean;
          };
      }

      • between: (params: { max: Money; min: Money; value: Money }) => boolean

        Check if a money amount is between a minimum and maximum amount (inclusive).

        If the currencies of the money amounts are different.

        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")
      • equal: (money1: Money, money2: Money) => boolean

        Check if money1 is equal to money2.

        If the currencies of the two money amounts are different.

        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")
      • greaterThan: (money1: Money, money2: Money) => boolean

        Check if money1 is greater than money2.

        If the currencies of the two money amounts are different.

        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")
      • greaterThanOrEqual: (money1: Money, money2: Money) => boolean

        Check if money1 is greater than or equal to money2.

        If the currencies of the two money amounts are different.

        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")
      • lessThan: (money1: Money, money2: Money) => boolean

        Check if money1 is less than money2.

        If the currencies of the two money amounts are different.

        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")
      • lessThanOrEqual: (money1: Money, money2: Money) => boolean

        Check if money1 is less than or equal to money2.

        If the currencies of the two money amounts are different.

        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")
      • Money: (obj: unknown) => boolean

        Type guard to check if an object is a Money object.

        Money.is.Money({ cents: 100, currency: "USD" }); // true
        Money.is.Money({ amount: 100, currency: "USD" }); // false
      • sameCurrency: (money1: Money, money2: Money) => boolean

        Check if money1 and money2 are in the same currency.

        Money.is.sameCurrency({ cents: 54321, currency: "USD" }, { cents: 54321, currency: "USD" }); // true
        Money.is.sameCurrency({ cents: 54321, currency: "USD" }, { cents: 54321, currency: "GBP" }); // false
      • zero: (money?: null | Money) => boolean

        Check if money is zero.

        Money.is.zero({ cents: 0, currency: "USD" }); // true
        Money.is.zero({ cents: 2, currency: "USD" }); // false
        Money.is.zero(null); // true
        Money.is.zero(undefined); // true
      • get not(): {
            between: (params: { max: Money; min: Money; value: Money }) => boolean;
            equal: (money1: Money, money2: Money) => boolean;
            Money: (obj: unknown) => boolean;
            sameCurrency: (money1: Money, money2: Money) => boolean;
            zero: (money?: null | Money) => boolean;
        }

    Money

    Infinity: Money = ...

    The infinity money object.

    Money.Infinity; // { cents: Infinity, currency: "USD" }
    
    Zero: Money = ...

    The zero money object.

    Money.Zero; // { cents: 0, currency: "USD" }
    
    • Gets absolute value of a money object.

      Parameters

      • money: Money

        The money object.

      Returns Money

      The absolute value of the money object.

      Money.abs({ cents: -12345, currency: "USD" }); // { cents: 12345, currency: "USD" }
      
    • Add two or more money objects.

      Parameters

      • ...monies: Money[]

        The money objects.

      Returns Money

      The result of adding the money objects.

      If the currencies of the money amounts are different.

      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")
    • Divide a money object by a divisor.

      Parameters

      • money: Money

        The money object.

      • divisor: number

        The divisor.

      Returns Money

      The result of dividing the money object by the divisor.

      Money.divide({ cents: 12345, currency: "USD" }, 2); // { cents: 6173, currency: "USD" }
      Money.divide({ cents: 12345, currency: "USD" }, 0); // Error("Cannot divide by zero")
    • Formats a money object.

      Parameters

      Returns string

      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"
    • Converts a number or string amount to a money object.

      Parameters

      • amount: string | number

        The amount.

      • currency: string = Currency.USD

        The currency code.

      • Optionaloptions: { locale?: LocalesArgument }

        Optional parameters.

        • Optionallocale?: LocalesArgument

          The locale to use for formatting.

      Returns Money

      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" }
    • Returns a given currency symbol

      Parameters

      • currency: string

        The currency code, e.g: "USD"

      • options: { locale?: LocalesArgument } = {}

        The options for getting the currency symbol.

        • Optionallocale?: LocalesArgument

          The locale to use for formatting

      Returns string

      The currency symbol.

      Money.getCurrencySymbol("USD"); // "$"
      Money.getCurrencySymbol("EUR"); // "€"
      Money.getCurrencySymbol("AUD"); // "د.إ"
    • Get the maximum of two or more money objects.

      Parameters

      • ...monies: Money[]

        The money objects.

      Returns Money

      The maximum money object.

      If no money objects are provided.

      If the currencies of the money amounts are different.

      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")
    • Get the minimum of two or more money objects.

      Parameters

      • ...monies: Money[]

        The money objects.

      Returns Money

      The minimum money object.

      If no money objects are provided.

      If the currencies of the money amounts are different.

      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")
    • Multiply a money object by a multiplier.

      Parameters

      • money: Money

        The money object.

      • multiplier: number

        The multiplier.

      Returns Money

      The result of multiplying the money object by the multiplier.

      Money.multiply({ cents: 12345, currency: "USD" }, 2); // { cents: 24690, currency: "USD" }
      Money.multiply({ cents: 12345, currency: "USD" }, 2.7); // { cents: 33332, currency: "USD" }
    • Calculate the percentage of one Money object relative to another.

      Parameters

      • part: Money

        The part Money object.

      • whole: Money

        The whole Money object.

      Returns number

      The calculated percentage as a string.

      If the currencies of the two money amounts are different.

      Money.percentage({ cents: 5000, currency: "USD" }, { cents: 10000, currency: "USD" }); // "50.0"
      Money.percentage({ cents: 5000, currency: "USD" }, { cents: 10000, currency: "GBP" }); // Error("Cannot perform operations on money objects in different currencies")
    • Subtract money2 from money1.

      Parameters

      • money1: Money

        The first money object.

      • money2: Money

        The second money object.

      Returns Money

      The result of subtracting money2 from money1.

      If the currencies of the two money amounts are different.

      Money.subtract({ cents: 54321, currency: "USD" }, { cents: 12345, currency: "USD" }); // { cents: 41976, currency: "USD" }
      Money.subtract({ cents: 54321, currency: "EUR" }, { cents: 12345, currency: "USD" }); // Error("Cannot perform operations on money objects in different currencies")
    • Converts a money object to an amount.

      Parameters

      • money: Money

        The money object.

      • Optionaloptions: { locale?: LocalesArgument }

        Optional parameters.

        • Optionallocale?: LocalesArgument

          The locale to use for formatting.

      Returns number

      The amount.

      Money.toAmount({ cents: 12345, currency: "USD" }); // 123.45
      Money.toAmount({ cents: 123456, currency: "EUR" }, { locale: "de-DE" }); // "1.234,56 €"
    • Converts a money object to a string amount.

      Parameters

      • money: undefined | null | Money

        The money object.

      Returns string

      The string amount.

      Money.toAmountString({ cents: 12345, currency: "USD" }); // "123.45"
      Money.toAmountString(null); // ""
      Money.toAmountString(undefined); // ""