Why Every Browser Calculator Gets 0.1 + 0.2 Wrong (and How to Fix It)
August 24, 2026 · DevTools
Open the console in any browser and type 0.1 + 0.2. You will not get 0.3. You will get 0.30000000000000004. This is not a JavaScript bug — it is IEEE 754 double-precision binary floating-point arithmetic, the standard every CPU follows. And if a web calculator is built on plain JavaScript Number math, it inherits the artifact on every single calculation.
Why binary floats can't hold 0.1
Computers store numbers in binary. In base 2, 0.1 is a repeating fraction — exactly like 1/3 = 0.333… in decimal. A 64-bit float keeps 52 bits of mantissa, so 0.1 gets rounded to the nearest representable value, which is 0.1000000000000000055511151231257827…. Add two slightly-wrong numbers and the error shows up in the 17th significant digit.
For most code this is fine: measurements carry more noise than 1e-17. But it leaks into visible places constantly:
- Money:
0.1 + 0.2 !== 0.3fails, soif (total === expected)silently branches the wrong way. - Comparisons and tests: snapshot tests on computed values flip between machines.
- Chained arithmetic: errors compound — subtract two nearly-equal large numbers and you can lose most of your significant digits (catastrophic cancellation).
- Exact answers: students expect
1/3to be exactly1/3, not0.3333333333333333.
The fix: arbitrary-precision decimal arithmetic
The solution is to stop using binary floats. Arbitrary-precision libraries like decimal.js store numbers as decimal digits with an explicit precision setting — 16, 32, 64 significant digits, whatever you need. 0.1 becomes exactly 0.1, because decimal can represent it perfectly. Factorials stay exact too: 100! keeps all 158 digits instead of degrading into 9.33262154439441e+157.
That is exactly how the Scientific & Engineering Calculator handles it: flip the Precision selector to 16–64 digits and the engine swaps from float64 to decimal arithmetic, so 0.1 + 0.2 = 0.3 — provably, every time. Float mode stays available when you specifically want to see what stock JavaScript would do.
When you actually want the artifact
There is one honest reason to keep float mode: you are debugging JavaScript itself. If your production code computes with Number, your calculator should be able to reproduce its exact (slightly wrong) output — otherwise you are verifying different math than you ship. A good calculator shows both, and makes the difference visible instead of hiding it behind rounding-to-display, which is what most web calculators do. That silent display rounding is the worst option of all: the UI says 0.3 while the underlying value is 0.30000000000000004, and the mismatch surfaces later in a comparison.
Three quick rules
- Never compare floats with
===. Compare with a tolerance:Math.abs(a - b) < 1e-9. - Never accumulate money in floats. Use integer cents, or decimal arithmetic.
- Distrust calculators that always look clean. If every result is suspiciously round, the tool is formatting the error away, not fixing it.
The next time a calculator tells you 0.1 + 0.2 = 0.3 in JavaScript, ask it for 40 digits. If it can't produce them, it's rounding — ours isn't.