Never store money in a float: integer paise and basis-point taxes

· by Krish Kumar
moneytypescriptposcorrectnessrounding

Every billing system I have replaced had the same bug waiting in it, and it is always the same bug: an amount stored as a floating-point number.

The canonical demonstration costs one line:

0.1 + 0.2 === 0.3   // false
0.1 + 0.2           // 0.30000000000000004

This is not a JavaScript quirk. IEEE 754 binary floating point cannot represent one tenth exactly, for the same reason decimal cannot represent one third exactly. Any language using doubles has it. The usual response is to round at the end and move on, and for a while that works, because a single bill is small and the error hides below the smallest coin.

It stops working when you sum. A till doing a few thousand lines a day across twelve outlets accumulates those errors into a figure that is visible in the day's closing balance and impossible to explain to an accountant. The error is never large. It is simply unaccountable, which is worse.

Store the smallest unit as an integer

In k-pos, the offline-first POS I built to replace a paid SaaS till, no amount is ever a decimal. Every amount is an integer count of paise — the smallest unit of Indian currency, one hundredth of a rupee.

// ₹249.50 is not 249.50
const price = 24950;  // paise, always an integer

Integers add and multiply exactly. A sum of ten thousand integer line amounts is exact. The rendering layer divides by 100 once, at the very last moment, purely to put a string in front of a human. Nothing downstream of that string is ever read back as a number.

SQLite made this easy to enforce: the column is INTEGER, so a decimal cannot be written even by accident. A schema constraint is worth more than a convention, because the convention only holds while everybody remembers it.

Tax rates are basis points, not percentages

The same reasoning applies one level up. GST in India is 5%, 12%, 18% — but a restaurant also deals with per-item cess and composite rates that are not whole numbers. Storing 0.05 as a rate reintroduces exactly the problem we just removed.

So rates are stored in basis points: one basis point is one hundredth of one percent.

// 5% = 500 bp, 18% = 1800 bp, 2.5% = 250 bp
const GST_5  = 500;
const GST_18 = 1800;

function taxOn(amountPaise: number, rateBp: number): number {
  // multiply first, divide last — never the reverse
  return Math.round((amountPaise * rateBp) / 10_000);
}

The ordering in that function is the whole point. amount * rate / 10000 keeps full integer precision until the single division. amount * (rate / 10000) computes a float first and hands you back the problem you were avoiding.

Round once, and define which way

There is exactly one rounding step, in taxOn, and it needs a stated rule. I use half-up, because that is what the tax authority's own worked examples use, and because a bill that disagrees with the inspector's arithmetic is a conversation you do not want to have.

Banker's rounding — half-to-even — is better for statistical neutrality over very large sample sizes. It is the wrong choice here. Matching the regulator beats matching the textbook.

The rule that matters more than which rounding you pick: round once. If tax is rounded per line and the lines are then summed, the bill total can differ from tax computed on the order total. Both numbers are defensible, they are just not equal, and whichever one you print is the one you must store. I round per line, because that is what appears on the printed bill, and the stored total is the sum of the stored lines. The invariant is that the ledger reproduces the paper.

Price snapshots

One more thing that looks unrelated and is not. An order line does not hold a foreign key to the current price of an item. It holds a copy of the price at the moment the line was created.

If it held a reference, then raising the price of a samosa tomorrow would silently rewrite every historical bill that contained one. Last month's sales report would change value every time the menu changed. I have seen this in production systems and the symptom is always the same: a report that was correct on Monday is different on Friday, and nobody can say why.

Snapshot the price. The storage cost is a few bytes per line. The alternative is a ledger that is not a ledger.

What this buys

None of this is clever. It is a set of boring decisions taken early:

The payoff is that the day's totals reconcile exactly, every day, without anyone needing to investigate a two-rupee discrepancy. In a system handling money, "exactly" is the only acceptable tolerance — not because two rupees matters, but because an unexplained two rupees means you do not know what your system is doing.