Class Money

Hierarchy

Implements

Constructors

Methods - Static Constructor

Methods - Getter

Methods - Comparison

Methods - Arithmetic

Methods - Formatting

Methods - Internal

Constructors

  • Create a new Money object.

    Throws

    • TypeError on non-integer amount values
    • RangeError on unsupported currency

    Example

    const fiveDollars = new Money('USD', 500)
    fiveDollars.getCurrency() // => 'USD'
    fiveDollars.getAmount() // => 500
    fiveDollars.toLocaleString() // => '$5.00'

    Parameters

    • currency: ISOCurrencyCode

      Currency code, e.g. "USD", "EUR", "GBP"

    • amount: number

      Integer value in the currency's smallest unit (e.g. cents for USD)

    Returns Money

Static Constructor Methods

  • Create a new Money object from a floating-point value. The value will be automatically rounded to the currency's smallest unit. The value will only be precise to the currency's smallest unit (e.g. USD$18.98123 will be rounded to USD$18.98)

    Throws

    • TypeError on non-float amount values.
    • RangeError on unsupported currency.

    Example

    const fiveDollarsAndSeventyCents = Money.fromFloat('USD', 5.70)
    fiveDollarsAndSeventyCents.getCurrency() // => 'USD'
    fiveDollarsAndSeventyCents.getAmount() // => 570
    fiveDollarsAndSeventyCents.toLocaleString() // => '$5.70'

    Parameters

    • currency: ISOCurrencyCode

      Currency code, e.g. "USD", "EUR", "GBP"

    • amount: number

      Floating-point value (e.g. 99.99)

    Returns Money

  • Create a new Money object from a string. Supports both integer and floating-point string values. If the amount is a floating-point, it will be automatically rounded to the currency's smallest unit. The value will only be precise to the currency's smallest unit (e.g. USD$18.98123 will be rounded to USD$18.98)

    Throws

    • TypeError on incorrect amount values.
    • RangeError on unsupported currency.

    Example

    // Float
    const fiveDollarsAndSeventyCents = Money.fromString('USD', '5.70')
    fiveDollarsAndSeventyCents.getCurrency() // => 'USD'
    fiveDollarsAndSeventyCents.getAmount() // => 570
    fiveDollarsAndSeventyCents.toLocaleString() // => '$5.70'
    // Int
    const eightyEur = Money.fromString('EUR', '8000')
    eightyEur.getCurrency() // => 'EUR'
    eightyEur.getAmount() // => 8000
    eightyEur.toLocaleString() // => '€80.00'

    Parameters

    • currency: ISOCurrencyCode

      Currency code, e.g. "USD", "EUR", "GBP".

    • amount: string

      String amount value (can either be integer or a float)

    Returns Money

Getter Methods

Comparison Methods

  • Returns

    True if input is identical to this Money, otherwise false.

    Throws

    • TypeError if input is not a Money.

    Example

    const a = new Money('USD', 2000)
    const b = new Money('USD', 1000)
    a.equals(b) // => false
    const c = new Money('USD', 2000)
    a.equals(c) // => true
    const d = new Money('EUR', 2000)
    c.equals(d) // => false

    Parameters

    Returns boolean

  • Returns

    True if input is greater than this Money, otherwise false.

    Throws

    • TypeError if input is not a Money.

    Example

    const a = new Money('USD', 2000)
    const b = new Money('USD', 1000)
    a.greaterThan(b) // => true
    b.greaterThan(a) // => false
    a.greaterThan(a) // => false

    Parameters

    Returns boolean

  • Returns

    True if input is greater than or equal to this Money, otherwise false.

    Throws

    • TypeError if input is not a Money.

    Example

    const a = new Money('USD', 1900)
    const b = new Money('USD', 2000)
    const c = new Money('USD', 2100)
    a.greaterThanOrEqual(b) // => false
    b.greaterThanOrEqual(a) // => true
    a.greaterThanOrEqual(a) // => true
    c.greaterThanOrEqual(b) // => true

    Parameters

    Returns boolean

  • Returns

    True if input is less than this Money, otherwise false.

    Throws

    • TypeError if input is not a Money.

    Example

    const a = new Money('USD', 1000)
    const b = new Money('USD', 2000)
    a.lessThan(b) // => true
    b.lessThan(a) // => false
    a.lessThan(a) // => false

    Parameters

    Returns boolean

  • Returns

    True if input is less than or equal to this Money, otherwise false.

    Throws

    • TypeError if input is not a Money.

    Example

    const a = new Money('USD', 1900)
    const b = new Money('USD', 2000)
    const c = new Money('USD', 2100)
    a.lessThanOrEqual(b) // => true
    b.lessThanOrEqual(a) // => false
    a.lessThanOrEqual(a) // => true
    c.lessThanOrEqual(b) // => false

    Parameters

    Returns boolean

Arithmetic Methods

  • Returns

    A new Money holding the sum of this Money and input(s).

    Throws

    • TypeError if input(s) is not a Money.
    • RangeError if currencies are not identical.

    Example

    const fiveUsd = new Money('USD', 500)
    const tenUsd = new Money('USD', 1000)
    const sum0 = fiveUsd.add(tenUsd) // $5 + $10
    sum0.getAmount() // => 1500
    sum0.toLocaleString() // => '$15.00'
    const fiftyUsd = new Money('USD', 5000)
    const sum1 = fiveUsd.add(tenUsd, fiftyUsd) // $5 + $10 + $50
    sum1.getAmount() // => 6500
    sum1.toLocaleString() // => '$65.00'

    Parameters

    • Rest ...addends: Money[]

    Returns Money

  • Returns

    A new Money holding the quotient of this Money and input.

    Example

    const twentyYuan = new Money('CNY', 2000)
    const quotient = twentyYuan.divide(8) // CN¥20 / 8
    quotient.getAmount() // => 250
    quotient.toLocaleString() // => 'CN¥2.50'

    Parameters

    • divisor: number

    Returns Money

  • Returns

    A new Money holding the product of this Money and input.

    Example

    const fiveYen = new Money('JPY', 500)
    const product = fiveYen.multiply(9) // ¥500 * 9
    product.getAmount() // => 4500
    product.toLocaleString() // => '¥4,500'

    Parameters

    • multiplier: number

    Returns Money

  • Returns

    A new Money holding the difference between this Money and input(s).

    Throws

    • TypeError if input(s) is not a Money.
    • RangeError if currencies are not identical.

    Example

    const tenEur = new Money('EUR', 1000)
    const fiveEur = new Money('EUR', 500)
    const diff0 = tenEur.subtract(fiveEur) // €10 - €5
    diff0.getAmount() // => 500
    diff0.toLocaleString() // => '€5.00'
    const fiftyEur = new Money('EUR', 5000)
    const diff1 = tenEur.subtract(fiftyEur, fiveEur) // €10 - €50 - €5
    diff1.getAmount() // => -4500
    diff1.toLocaleString() // => '-€45.00'

    Parameters

    • Rest ...subtrahends: Money[]

    Returns Money

Formatting Methods

  • Convert Money to JSON.

    Returns

    JSON object.

    Example

    const tenDollars = new Money('USD', 1000)
    tenDollars.toJSON() // => { currency: 'USD', amount: 1000 }

    Returns {
        amount: number;
        currency: ISOCurrencyCode;
    }

  • Formats this Money object to locale-aware string with its currency symbol, using Intl.NumberFormat.

    Returns

    The string-formatted Money with its currency symbol (e.g. "$49.99", "9,99 €").

    Example

    const tenDollars = new Money('USD', 1000)
    // Using default locale (`en-US`):
    tenDollars.toLocaleString() // => $10.00
    // Using different locale:
    tenDollars.toLocaleString('fr-CA') // => 10,00 $ US

    Parameters

    • Optional locale: string

      Optional. Defaults to en-US.

    Returns string

Internal Methods

Generated using TypeDoc