@bitzlato/money-js
This is a small library for working with monetary values. Based on big.js and follows Martin Fowler's Money Type.
Install
npm i @bitzlato/money-js
Usage
Creating a money object
The library does not contain information about currencies, you have to define them yourself.
const USD: Currency = {
code: 'USD',
minorUnit: 2,
};
const BTC: Currency = {
code: 'BTC',
minorUnit: 8,
};
import { Money } from '@bitzlato/money-js';
const dollars = Money.fromDecimal(10.25, USD);
const bitcoins = Money.fromDecimal('0.05316732', BTC);
bitcoins.toCents();
const tenDollars = Money.fromCents(1000, USD);
const fiveBitcoins = Money.fromCents('500000000', BTC);
fiveBitcoins.toString();
const fiveDollars = new Money(new Big('5'), USD);
JSON.stringify(fiveDollars);
Arithmetic
const tenDollars = Money.fromCents(1_000, USD);
tenDollars.add(Money.fromCents(250, USD));
tenDollars.subtract(Money.fromCents(250, USD));
tenDollars.multiply(2.5);
tenDollars.divide(2.5);
Adding or subtracting different currencies will cause an error:
Money.fromCents(1_000, USD).add(Money.fromCents(200_000, BTC));
Comparing
const tenDollars = Money.fromCents(1_000, USD);
const bigger = Money.fromCents(1_500, USD);
const smaller = Money.fromCents(500, USD);
const equal = Money.fromDecimal(10, USD);
const bitcoins = Money.fromCents(200_000, BTC);
tenDollars.equals(bitcoins);
tenDollars.equals(equal);
tenDollars.compare(bigger);
tenDollars.compare(smaller);
tenDollars.compare(equal);
tenDollars.compare(bitcoins);
tenDollars.lessThan(bigger);
tenDollars.lessThanOrEqual(equal);
tenDollars.lessThanOrEqual(smaller);
tenDollars.greaterThan(smaller);
tenDollars.greaterThanOrEqual(equal);
tenDollars.greaterThanOrEqual(bigger);
Asserts
Money.fromCents(0, USD).isZero();
Money.fromCents(100, USD).isZero();
const dollars = Money.fromCents(-100, USD);
dollars.isPositive();
dollars.isNegative();
const zero = Money.fromCents(0, USD);
zero.isNegative();
zero.isPositive();
Convert
Money.fromCents(100_000_000, BTC).convert(60_737, USD).toString();
Formatting
Money.fromCents(12_340_000_000, USD).toFormat();
Money.fromDecimal('1000.00010000', BTC).toFormat();
Money.fromDecimal('10230.0', BTC).toFormat();
Money.fromDecimal('1000000', BTC).toFormat();
Formatting options:
interface FormatOptions {
minFractionDigits?: number;
maxFractionDigits?: number;
removeTrailingFractionalZeros?: boolean;
decimalSeparator?: string;
groupSeparator?: string;
groupSize?: number;
secondaryGroupSize?: number;
fractionGroupSeparator?: string;
fractionGroupSize?: number;
}
const bitcoins = Money.fromDecimal('1330000.128345', BTC);
const dollars = Money.fromDecimal('0.128345', USD);
bitcoins.toFormat({
decimalSeparator: ',',
groupSeparator: ' ',
});
bitcoins.toFormat({ maxFractionDigits: 4 });
dollars.toFormat({ maxFractionDigits: 6 });
dollars.toFormat({ minFractionDigits: 0, maxFractionDigits: 1 });
bitcoins.toFormat({ removeTrailingFractionalZeros: false });
Rounding
Money.ROUND_HALF_UP
Money.ROUND_HALF_EVEN
- The default. Bankers Rounding.Money.ROUND_DOWN
Money.ROUND_UP
You can specify any of these roundings when creating:
Money.fromDecimal(10.2548, USD, Money.ROUND_UP).toString();
Test
npm test
License
MIT.