Money and rounding
Amounts are exact fractions. Rounding is a decision, so it has a name and happens where you put it.
amount("$1,234.56"); // 30864/25
amount("4.5%"); // 9/200
round("2.345"); // 117/50 = 2.34
round("2.345", { mode: "half-up" }); // 47/20 = 2.35
format("1234567.891"); // "1,234,567.89"
allocate(100, [1, 1, 1]); // 33.34, 33.33, 33.33
Why not floating point
One tenth has no finite binary expansion, so 0.1 in a double is 0.1000000000000000055…; ten of them do not make 1, and a column of such numbers need not add to its total. The error is tiny each time but it is not zero, and rounding to the cent at the end can then go the wrong way. With rationals and nothing else is possible.
Rounding rules
When an exact amount such as 1498.876313… must become a payable one, a rule is needed for the half-way cases and for the direction:
| mode | 2.345 | 2.355 | −2.345 | meaning |
|---|---|---|---|---|
| half-even | 2.34 | 2.36 | −2.34 | ties go to the even digit (banker's rounding); the default, since ties then cancel on average |
| half-up | 2.35 | 2.36 | −2.35 | ties go away from zero (the commercial rule) |
| half-down | 2.34 | 2.35 | −2.34 | ties go towards zero |
| up / down | 2.35 / 2.34 | 2.36 / 2.35 | −2.35 / −2.34 | always away from / towards zero |
| floor / ceil | 2.34 / 2.35 | 2.35 / 2.36 | −2.35 / −2.34 | always towards −∞ / +∞ |
places may be negative: round(1250, { places: -2 }) is 1200 under half-even. Functions that produce schedules take a rounding option: { places, mode }, a number of places, a mode, or false for none.
Rounding goes through Core's rounding(), so a mode can also be any Core mode name (half-odd, toward-zero, ...), a rounder made by rounding(...), or your own function given { value, floor, ceil, fraction } that returns floor or ceil. The default is the exported constant MONEY_ROUNDING, half-even. Core itself rounds half up by default; finance keeps banker's rounding because that is the usual default in accounting software. Many tax and currency-conversion rules require half-up instead: name it where they apply.
Splitting without losing a cent
A third of 100.00 is 33.33⅓. Rounding each share gives 99.99 or 100.01. allocate rounds every share down and hands out the cents left over to the shares that lost most (the largest-remainder method), so the parts always add to the total.
Showing numbers
format(x, { places, mode, currency, separator, percent }) writes an amount for people. show(x, places) is the plain version used throughout these pages, and it also accepts an interval: if both ends round to the same decimal, that decimal is the correctly rounded answer and is printed; if a rounding boundary lies inside the interval, both ends are printed, and asking for more digits from the function that made it will settle the matter.
| call | returns |
|---|---|
| amount(x), rate(x) | Rational |
| round(x, { places = 2, mode = "half-even" }) | Rational |
| format(x, options), show(x, places) | string |
| allocate(total, weights, { places }) | Rational[] that add to the total |
| power(base, exponent, digits) | Rational when exact, else RationalInterval |
@ratmath/finance and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.