Precise money and currency arithmetic
npm install @philiprehberger/money-tsPrecise money and currency arithmetic
npm install @philiprehberger/money-ts
import { money } from '@philiprehberger/money-ts';
const price = money(1099, 'USD');
const tax = price.percentage(8.5);
const total = price.add(tax);
total.format(); // "$11.92"
total.format('de-DE'); // "11,92 $"
const [a, b] = total.allocate([60, 40]); // handles remainders
import { money } from '@philiprehberger/money-ts';
const price = money.fromDecimal(10.99, 'USD');
price.toCents(); // 1099
| Method | Description |
|---|---|
money(cents, currency) | Create from minor units |
money.fromDecimal(decimal, currency) | Create from decimal |
.add(other) / .subtract(other) | Arithmetic (same currency) |
.multiply(n) / .divide(n) / .percentage(pct) | Scaling |
.allocate(ratios) | Split with remainder handling |
.format(locale?) | Intl.NumberFormat currency string |
.toDecimal() / .toCents() | Conversion |
.equals() / .greaterThan() / .lessThan() | Comparison |
npm install
npm run build
npm test
If you find this project useful: