Lightweight fixed-point Money type and helpers for financial calculations.
npm install asljs-moneyMoney, a type that the library provides, represents a fixed-point decimal
asljs-money named export money creates a new instance of Money from
Money instance. It also
Money instance from a variety of sources:
-1,234.56, use money.parse(value) or
money.fromString(value).
money.fromMinor(value).
money.fromNumber(value).
money.fromMajor(value).
Money instance provides these operations:
value - returns an integer representation of this Money instance.
add(money1, money2, ...) - returns a new instance of Money with added
distribute(recipients, unit) - divides the amount among recipients,
unit (default is minor).
major() - returns a number of major units (dollars) as an integer JavaScript
1 for 1.23.
minor() - returns a number of minor units (dollars) as an integer JavaScript
23 for 1.23.
subtract(money1, money2, ...) - returns a new instance of Money with
toNumber() - returns a floating-point number representation of the Money
toString() - returns string representation of the Money instance, e.g.
-1,234.56.
convert(rate, currency) - converts the amount to another currency using a
minor - indicates that computations are done in minor units (cents).
major - indicates that computations are done in major units (dollars).
zero - zero value.
money provides these utility functions:
isMoney(value) - returns true if value is an instance of Money.
money (ESM):
js
import { money } from 'asljs-money';
`
Create a Money instance, distribute it among shareholders, and sum up the result:
`js
const amount =
money.fromString('1,204.20');
const dividends =
amount.distribute([0.6, 0.2, 0.2]);
const total =
dividends.reduce(
(sum, item) =>
sum.add(item),
money.zero);
console.log(total.toString());
`
Currency-aware operations:
`js
const usd = money.fromMinor(10000, 'USD');
const eur = usd.convert(0.9, 'EUR');
console.log(usd.toString()); // "100.00 USD"
console.log(eur.toString()); // "90.00 EUR"
``