If you have ever typed 0.1 + 0.2 into a browser console, you know the punchline: 0.30000000000000004. It is the most repeated joke in programming. It is also a real bug when the number on screen is somebody's invoice total.
I work on a site full of small calculators (loan payments, discounts, sales tax, rounding), so this comes up a lot. Here is what actually goes wrong, with numbers I ran myself, and the habits that stop it.
What is happening
JavaScript numbers are IEEE 754 double-precision floats. They store values in binary, and many everyday decimals have no exact binary form. Print 0.1 and 0.2 with 20 decimal places and you see what is really stored:
(0.1).toFixed(20) // "0.10000000000000000555" (0.2).toFixed(20) // "0.20000000000000001110" (0.1 + 0.2).toFixed(20) // "0.30000000000000004441" Each value is a tiny bit off. Add them and the errors do not cancel. The result is about 5.55e-17 away from 0.3, which is enough that 0.1 + 0.2 === 0.3 is false.
One error that small is harmless. The trouble is that errors pile up, and that rounding looks at the exact stored value.
Problem 1: errors accumulate
Add one cent a thousand times:
let total = 0; for (let i = 0; i < 1000; i++) total += 0.01; console.log(total); // 9.999999999999831 You expect 10. You get 9.999999999999831. Run the same loop ten million times and the sum is 99999.99998630969 instead of 100000. Python gives identical results, because this is the float format, not the language.
Other classic ones, all checked in Node:
1.15 * 100 // 114.99999999999999 0.07 * 100 // 7.000000000000001 1.1 * 3 // 3.3000000000000003 That 1.15 * 100 is the one that bites people converting dollars to cents. Math.floor(1.15 * 100) gives 114. One cent lost, no error thrown.
Problem 2: rounding lands on the wrong side
This is the one users actually notice. Take 1.005 and round it to two places, the way a person would: the third digit is a 5, so round up to 1.01.
(1.005).toFixed(2) // "1.00" Math.round(1.005 * 100) / 100 // 1 Both are wrong by a human's standard. Look at the stored value:
(1.005).toFixed(20) // "1.00499999999999989342" The number is actually just below 1.005, so "round half up" never sees a tie. The code did exactly what it should with the value it had. The value was not the one you typed.
More examples from my own run (Node 24 and Python 3):
| Typed value | Stored as (approx.) | toFixed(2) | What a person expects |
|---|---|---|---|
| 1.005 | 1.00499999999999989 | 1.00 | 1.01 |
| 2.675 | 2.67499999999999982 | 2.67 | 2.68 |
| 1.255 | 1.25499999999999989 | 1.25 | 1.26 |
| 2.345 | 2.34500000000000020 | 2.35 | 2.35 |
The last row works only because 2.345 happens to be stored slightly above the tie. That is the nasty part: the same code is right for some inputs and wrong for others, so a handful of spot checks will not catch it.
Problem 3: it changes real money
Say a cart has three items at $19.99 with 8.25% sales tax. Subtotal in floats is 19.99 * 3 = 59.97. Now a single item at $19.99 taxed at 8.25%:
19.99 * 0.0825 // 1.649175 Whether you show 1.65 per item or tax the whole cart and round once changes the total. Tax the cart: 59.97 * 0.0825 = 4.947525, which rounds to 4.95. Tax each item and round first: 1.65 * 3 = 4.95. Here they agree, but change the rate or price and they will not, and your total will not match the receipt from the store. Decide where you round, write it down, and test it. (If you want to see a cart-style example worked out, a sales tax calculator is a handy thing to compare against.)
Loan math shows the same thing at a smaller scale. A $250,000 loan at 6.5% for 30 years comes to $1,580.17 a month. Python's Decimal gives 1580.170058732409..., plain floats give 1580.1700587324133. Identical to the cent, different after the tenth decimal. It only matters once you chain hundreds of rounded steps, like an amortization schedule.
Three habits that fix most of it
1. Work in integer cents (or the smallest unit). Store 1999, not 19.99. Integer addition and subtraction up to Number.MAX_SAFE_INTEGER (2^53 − 1, about 9.007 quadrillion) is exact.
const priceCents = 1999; const qty = 3; const subtotalCents = priceCents * qty; // 5997, exact const taxCents = Math.round(subtotalCents * 825 / 10000); // 495 Notice the rate is 825 / 10000, not 0.0825. Multiply first, divide last, and keep the divisor a power of ten you control.
2. Round once, at a place you chose. Do not round after every intermediate step unless the real-world rule says to, and write the rule in a comment.
3. For decimal-heavy work, use a decimal type. Python has decimal.Decimal. In JavaScript, libraries like decimal.js or big.js do the same job. Check that you pass them strings, not floats:
from decimal import Decimal, ROUND_HALF_UP Decimal('1.005').quantize(Decimal('0.01'), ROUND_HALF_UP) # Decimal('1.01') Decimal(1.005).quantize(Decimal('0.01'), ROUND_HALF_UP) # Decimal('1.00') The second line is the trap: Decimal(1.005) faithfully copies the already-wrong float. Strings are exact.
If you must stay with plain numbers for a quick fix, adding a tiny epsilon helps (Math.round((1.005 + Number.EPSILON) * 100) / 100 gives 1.01), but it is a patch, not a guarantee. It can misround other inputs, so test it against known cases before trusting it.
Banker's rounding is a separate question
Another source of "wrong" answers is the rounding rule. Half-up always rounds .5 up. Half-even (banker's rounding) rounds to the nearest even digit, so 2.5 becomes 2 and 3.5 becomes 4. Python's round() uses half-even, which is why people are surprised that round(2.5) is 2. Neither is a bug. They are different rules, and a finance or tax rule may require a specific one. If you want to check a half-up result by hand, a rounding calculator is a quick way to compare against your own code's output.
A testing habit worth stealing
Before writing a money formula, work one example by hand, ideally with numbers that make rounding tricky: a value ending in 5, a rate that does not divide cleanly. Then make the code reproduce that exact figure. A few hand-worked ties (1.005, 2.675), a value just below a tie (1.004), a big amount and a negative refund catch more bugs than a hundred random inputs, because random inputs rarely land on a tie.
Wrap-up
Floats are fine for physics and graphics. For money, either store integers or use a decimal type, choose one rounding rule on purpose, and test against ties. The 0.30000000000000004 joke is funny right up until it is your customer's total.
What is the nastiest rounding bug you have shipped? I would like to hear it in the comments.
Disclosure: I work on MultiCalify, a free collection of online calculators, which is why the examples above lean toward loans, tax and rounding.
