Why you should never store money as a floating-point number
iotoms team · October 22, 2026 · 6 min read
A packaged foods distributor running six vans out of a regional depot built its first pricing calculator the way most small teams do: a spreadsheet-turned-script, quantities times unit price, tax on top, store the result as a decimal number. Nobody thought twice about it. The numbers were money, money had decimals, decimals were a float. It shipped, it worked, and it kept working for the better part of a year — until the owner's accountant flagged something during a routine bank reconciliation. A batch of forty-one invoices that should have summed to exactly $18,432.60 actually summed to $18,432.59 when the ledger software added them up. One cent. Nobody could find where it had gone.
The bug that only shows up at volume
The developer who'd written the calculator was confident it was fine, and in a narrow sense he was right — no single invoice was ever visibly wrong. Pull up any one of the forty-one and the math checked out to the penny. The discrepancy only appeared when you summed a large batch of them against an independently calculated total, which is exactly what a bank reconciliation does. That made it almost impossible to find by inspection: you can't spot a bug in a single row when the bug only exists in the aggregate.
The actual cause was that a double-precision float can't represent most decimal fractions exactly. $9.95 isn't stored as 9.95 — it's stored as the nearest value a binary fraction can reach, something like 9.949999999999999289. For any one price, that error is too small to notice; print it with two decimal places and it rounds back to 9.95 every time. But run that slightly-off value through multiplication (quantity times price), then addition (summing lines into a subtotal), then more rounding (tax application), and the tiny errors don't cancel out — they accumulate in whichever direction the binary representation happened to lean. Across one invoice, invisible. Across forty-one invoices and a few thousand line items, a one-cent gap that an accountant notices immediately, because accountants reconcile to the cent by profession.
Why the obvious fixes don't fix it
The first instinct is usually to round more aggressively — round every line, round every subtotal, round twice to be safe. That doesn't address the root cause, because the float still can't hold 9.95 exactly; it just moves the error to a different decimal place and changes which invoices are affected. The second instinct is to switch to a rounding function from a math library and call it done, which helps with display but not with storage — the value saved back to the database is still a binary approximation, so the next calculation that reads it back inherits the same error.
The actual fix is to stop representing money as a fractional number at all. A dollar amount isn't really a continuous quantity — it's a count of the smallest unit a currency uses, cents for dollars, paisa for rupees, fils for dirhams. Once you store $9.95 as the integer 995 instead of the float 9.95, every operation on it — multiply by a quantity, add a tax percentage, sum a hundred line items — is integer arithmetic. Integers don't have a representation gap. 995 is exactly 995, forever, no matter how many times you add it to something else.
What changes once money is an integer
Moving to integer minor units isn't just a storage change — it forces a few decisions that floats let you avoid making, which is exactly why it prevents bugs:
- Rounding has to happen at a named point, once. With floats, rounding error sneaks in everywhere by accident. With integers, you decide explicitly: round when calculating tax on a line, not before, not again later. Pick one rule — most financial systems use round-half-to-even ("banker's rounding") specifically because it doesn't bias totals upward or downward over many transactions — and apply it in exactly one place.
- Multiplication needs a defined order. Quantity times unit price times tax rate gives a different last-cent result depending on whether you round after each step or only at the end. That's true whether you use floats or integers, but integers make you confront the choice instead of letting a float silently pick one for you.
- Division needs an explicit remainder rule. Splitting one $100 payment across three $33.34 invoices leaves two cents unaccounted for no matter what type you use. The fix isn't a data type, it's a rule — allocate the leftover cents to the largest invoice, or the first one, or proportionally — and write it down so every developer who touches the code applies the same rule.
- Display is a formatting step, not a storage format. The integer 995 only becomes "$9.95" when you render it. Keeping that conversion at the edge — the UI, the printed invoice, the API response — means the stored value never degrades no matter how many times it passes through a screen or a report.
None of this is exotic engineering. It's the same reason every mature payment processor, point-of-sale system, and accounting package represents money as an integer count of the smallest unit, never a raw decimal or float. It costs a small amount of discipline up front — remembering that 995 means $9.95, writing one shared formatting function instead of letting every screen round its own way — in exchange for a category of bug that otherwise reappears every time transaction volume grows past the point where nobody's eyeballing each invoice anymore.
Practical checklist
- Store every money value as an integer count of the smallest currency unit (cents, paisa, fils) — never as
floatordouble. - Keep the currency code next to the amount; "100" means different things for different currencies.
- Pick one rounding rule (round-half-to-even is the standard default) and apply it at one defined point per calculation, not scattered across the codebase.
- Write down the remainder rule for splits and allocations before you need it, not while debugging a one-cent discrepancy under deadline.
- Convert to a human-readable decimal only at the point of display — never store the formatted version.
- Test the specific values known to break binary floats (9.95, 0.1 + 0.2, and similar) as part of your regular test suite, not as an afterthought.
iotoms' field app and ledger do all money math in integer minor units end to end, from a quantity entered on a van with no signal to the invoice that prints at the counter, so a cent never has anywhere to quietly disappear to.