@vested/money
A Money wrapper class for node and the browser, built on Big.js, to make dealing with arbitrarily large amounts of money easy.
Each Money object is immutable, infiniately large, and infiniately small.
Installation
yarn add @vested/money
Example Usage
import { Money } from '@vested/money'
const oneFifty = new Money('1.50', 'USD')
const threeDollars = oneFifty.times(2)
const threeFifty = threeDollars.plus(50)
const fourDollars = threeFifty.roundUnit()
const oneDollar = fourDollars.minus(threeDollars)
const twoDollars = fourDollars.div(2)
threeDollars.gt(twoDollars)
threeDollars.lt(twoDollars)
fourDollars.eq(twoDollars.plus(twoDollars))
Methods
new Money(units, currency = 'USD): Money
Money.fromAmount(units, currency = 'USD'): Money
Builds a new Money object, taking the number of units (dollars) as the first argument and an optional currency string as a second argument (default 'USD'). For example, the following builds a $3.50 USD Money object:
const money = new Money(3.50, 'USD')
const money2 = Money.fromAmount('3.50', 'USD')
You can pass a string, number, or Big as the first argument.
Money.fromCents(units, currency = 'USD'): Money
Builds a new Money object, taking subunits (cents) as the first argument and an optional currency string as a second argument (default 'USD'). For example, the following builds a $3.50 USD Money object:
const money = Money.fromCents('350', 'USD')
You can pass a string, number, or Big as the first argument.
Money.fromJSON(): Money
Builds a new Money object from a given MoneyJSON object, generally created by the money.toJSON()
method below.
const money = Money.fromJSON({
units: '3.50',
cents: '350',
currency: 'USD'
})
Money.zero(currency = 'USD'): Money
Helper method to build a zero object of a given currency (default 'USD').
const money = Money.zero('USD')
money.cents: Big
Returns a Big object representing the cents of the Money object
const money = Money.fromAmount(3.50, 'EUR')
money.cents
money.currency: string
Returns a string representing the currency.
const money = Money.zero('EUR')
money.currency
money.units: Big
Returns a Big object representing the units of the Money object
const money = Money.fromCents(350, 'EUR')
money.units
money.toJSON(): MoneyJSON
Returns a MoneyJSON representation of a Money object
const money = Money.fromAmount('3.50')
money.toJSON()
money.toFixed(size = 2): string
Returns a string rounded to a given number of units, using half-up
rounding.
const money = Money.fromAmount('3.50')
money.toFixed()
money.toFixed(2)
money.toFixed(1)
money.toFixed(0)
money.roundToUnit(mode = 'half-up'): Money
Returns a new Money object rounded to the nearest unit, using the provided rounding mode.
const money = Money.fromAmount('4.50')
money.roundToUnit()
money.roundToUnit('down')
money.roundToUnit('up')
money.roundToUnit('half-up')
money.roundToUnit('half-even')
money.rountToCent(mode = 'half-up'): Money
Returns a new Money object rounded to the nearest cent, using the provided rounding mode.
const money = Money.fromAmount('0.985')
money.roundToCent()
money.roundToCent('down')
money.roundToCent('up')
money.roundToCent('half-up')
money.roundToCent('half-even')
money.plus(other): Money
Returns a new Money object by adding the other value. The argument must be another Money object.
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
const money2 = Money.fromAmount('2.00')
money.plus(money2)
money.minus(other): Money
Returns a new Money object by subtracting the other value. The argument must be another Money object.
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
const money2 = Money.fromAmount('2.00')
money.minus(money2)
money.times(other): Money
Returns a new Money object by multiplying the other value. The argument can be another Money object, a string, a number, or a Big.
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
money.times('2')
money.div(other): Money
Returns a new Money object by dividing by the other value. The argument can be another Money object, a string, a number, or a Big.
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
money.div('2')
money.eq(other): boolean
Returns true
if this Money object is equal to the other, and false
otherwise. The argument can be another Money object, a string (units), a number (cents), or a Big (cents).
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
money.eq('3.50')
money.eq(Money.fromCents('350'))
money.eq(1.75)
money.gt(other): boolean
Returns true
if this Money object is greater than the other, and false
otherwise. The argument can be another Money object, a string (units), a number (cents), or a Big (cents).
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
money.gt('1.75')
money.gt(Money.fromCents('350'))
money.gt('3.50')
money.lt(other): boolean
Returns true
if this Money object is less than the other, and false
otherwise. The argument can be another Money object, a string (units), a number (cents), or a Big (cents).
If a Money object of a different currency is provided, a CurrencyMismatchError
will be thrown.
const money = Money.fromAmount('3.50')
money.lt('5.00')
money.lt(Money.fromCents('350'))
money.lt('3.50')
money.lt('1.75')
About Vested
@vested/money is maintained by Vested. Vested is a free service helping employees understand, manage, and cash-in on their startup equity positions, regarless of seniority.