money-safe
v0.1.0TYPE-SAFE MONEY
DOCS INTERFACE · GUIDE / RECIPES / REFERENCE · MINOR UNITS STAY EXPLICIT
Rounding

Rounding

Rounding happens when a rate or division produces a fraction of a minor unit.

ts
amount('100.00', 'CNY').times('0.855', {
  rounding: 'half-up'
});

Modes

  • down: truncate toward zero.
  • up: round away from zero.
  • half-up: round half values away from zero.
  • half-even: round half values to the nearest even integer.

Prefer string ratios

ts
amount('19.99', 'USD').times('0.15');

String ratios preserve decimal intent better than floating-point literals in code review.

Allocation

Use split(count) for equal shares:

ts
amount('1.00', 'CNY').split(3).map((part) => part.toDecimal());
// ['0.34', '0.33', '0.33']

Use allocate(ratios) for weighted shares:

ts
amount('1.00', 'CNY').allocate([2, 1]).map((part) => part.toDecimal());
// ['0.67', '0.33']