Money Is Not a Float
The first rule of money in software: never store it as a floating-point number. $0.10 + $0.20 famously isn't $0.30 in binary. Store money as an exact integer count of the smallest unit.
Every serious money bug starts the same way: someone stored a price as a floating-point number. Floats can't represent most decimal fractions exactly, so 0.1 + 0.2 is 0.30000000000000004, not 0.3. On one calculation nobody notices; across millions of transactions the cents drift, totals stop matching, and an auditor finds money that appeared or vanished. This session is about the single most important habit in payment code: represent money exactly, as an integer number of the smallest unit.
The float trap
Run this in any language and watch the money leak:
0.1 + 0.2 # -> 0.30000000000000004
round(0.1 + 0.2, 2) # -> 0.3, but the error is only hidden, not gone
# multiply a fraction-of-a-cent error by millions of rows and it adds up19.99 literally does not exist in binary floating point; the computer stores the nearest representable number, which is slightly off. Every arithmetic step compounds the error. You can hide it with rounding on display, but the underlying value is already wrong, and the errors accumulate. For anything that must reconcile to the cent - prices, balances, invoices - floats are disqualified.Store money as integer minor units
The fix is simple and universal: store money as a whole number of the smallest unit the currency has - cents for dollars, pence for pounds, satoshi for Bitcoin - plus which currency it is.
| Amount | Wrong (float) | Right (minor units) |
|---|---|---|
| $19.99 | 19.99 | 1999 cents |
| $0.30 | 0.1 + 0.2 | 10 + 20 = 30 cents |
| ยฅ1,234 | 1234.0 | 1234 (yen has no minor unit) |
| 0.5 BTC | 0.5 | 50000000 satoshi |
As integers, arithmetic is exact: 10 + 20 is exactly 30, forever. Rounding only ever happens where you genuinely divide or take a percentage, and there you choose the rounding rule deliberately. The stack's <a href='/stack/larzmoney'>larzmoney</a> is built on exactly this: a Money is an integer count of minor units plus a currency, and it refuses float input loudly so the bug can't sneak in. Its companion <a href='/stack/larzcurrency'>larzcurrency</a> turns those integers into correct human-readable strings per locale.
The exact-money habit
- Never store or compute money as a float - the errors are real and compound.
- Store money as an integer count of the smallest unit, plus the currency.
- Round deliberately only where you divide or take a percentage.
- Use a money type (like larzmoney) that enforces this so you can't slip.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Never store money as a floating-point number - 0.1 + 0.2 isn't 0.3, and the errors compound.
- Store money as an exact integer count of the smallest unit (cents, pence, satoshi) plus a currency.
- Integer arithmetic is exact; round deliberately only where you divide or take a percentage.
- Use a money type like larzmoney that rejects floats so the bug can't enter your code.
Frequently asked
If I shouldn't use floats, can't I just round to 2 decimal places everywhere?
Rounding on display is good practice, but rounding does not fix the underlying problem, because the errors happen during calculation, before you round, and can accumulate in stored values. If you keep money in floats and round only when showing it, each intermediate calculation (adding tax, splitting a bill, applying a discount, summing a column) can still introduce or carry a tiny error, and if you store those rounded-but-still-float results and compute on them again, the drift continues. Rounding the final display hides one symptom but not the disease: the stored numbers are already slightly wrong, and a sum of many of them can be off by a cent or more, so totals won't reconcile. The robust fix is to not use floats for money at all - store amounts as exact integers (minor units), do all arithmetic on those integers where it's exact, and round explicitly and deliberately only at the specific points where a real division or percentage produces a fraction of a cent (and there, decide the rounding rule and make sure the pieces still sum to the whole). Then your stored values are always exact, your sums always reconcile, and display formatting is a separate, safe step. So rounding for display: yes; relying on rounding to make float money correct: no. Use integer minor units as the source of truth, and rounding becomes a controlled, occasional, deliberate operation rather than a constant patch over a leaky representation. That's the difference between money code that reconciles and money code that mysteriously drifts.
Languages have a Decimal type - isn't that enough instead of integer minor units?
A proper decimal type (like Python's Decimal, or SQL's DECIMAL/NUMERIC) is a legitimate and correct way to represent money, and it solves the core problem that floats don't: it represents decimal fractions exactly, so 0.1 + 0.2 really is 0.3 and there's no binary rounding error. If you use Decimal carefully (constructing from strings, not floats, and controlling its rounding context), it's a perfectly valid choice, and many systems use it. So decimals are not wrong. That said, integer minor units have some practical advantages that make them a popular default: they're the simplest possible representation (just an int), they're unambiguous about precision (cents means exactly cents), they map directly to how payment processors and accounting systems often work, they're trivially fast and storable in any database as a plain integer, and they make it impossible to accidentally introduce sub-cent precision you didn't intend. Decimals, by contrast, require care - if you ever construct a Decimal from a float you reintroduce the error, and you must manage the rounding context. Both approaches, used correctly, give exact money; the choice is somewhat a matter of ecosystem and preference. Integer minor units (the approach larzmoney takes) are a great default for their simplicity and directness, especially when paired with a type that enforces the discipline; decimals are equally valid when your language and database support them well and you use them carefully. The one thing that is always wrong is binary floating point. So the real rule isn't 'integers over decimals'; it's 'exact representation (integer minor units or a properly-used decimal) over floats, always.' Pick whichever exact approach fits your stack, and be consistent.
Related lessons
Splitting Money Fairly, to the Cent
Divide $10.00 three ways and you get $3.33 x 3 = $9.99 - someone eats a cent. Splitting moโฆ
Money & Payments in SoftwarePrices, Discounts & Tax
A checkout total is a small pipeline that's easy to get subtly wrong: subtotal, then discoโฆ
Personal FinanceBudgeting That Actually Sticks: 50/30/20 and Beyond
Most budgets fail because they're spreadsheets you abandon by week two. Here's a system siโฆ