Why 0.1 + 0.2 = 0.30000000000000004 (and the four fixes that actually work)
Every JavaScript developer runs into this eventually:
0.1 + 0.2 // 0.30000000000000004 0.1 + 0.2 === 0.3 // false It looks like a bug in JavaScript. It is not. It is how binary floating point works in every language that uses IEEE 754 doubles: JavaScript, Python, Java, C#, Go, Ruby, PHP, you name it. Here is what is actually going on, and the four fixes that stop it from biting you in production.
Why 0.1 + 0.2 is not 0.3
Computers store numbers in binary. Integers are exact: 1, 2, 3 have a clean binary form. Fractions usually do not.
In base 10 you cannot write 1/3 exactly. You get 0.333333... forever, and at some point you have to stop writing digits. Base 2 has the same problem, just with different numbers. 0.1 in binary is:
0.0001100110011001100110011001100110011... That pattern repeats forever. A 64-bit float keeps 53 bits of it and rounds the rest away, so the value stored is not 0.1. It is the closest binary fraction to 0.1, off by about 1e-17.
Do the same with 0.2, add the two approximations, and the small errors add up to something you can see:
0.1 + 0.2 // 0.30000000000000004 The error is tiny in absolute terms, but it is not zero, and === is exact.
Where this actually hurts
Most of the time the error is invisible. It becomes a problem when it meets an exact comparison or a loop:
- A total of 19.99 + 4.99 does not equal 24.98, so a discount check silently fails.
- A "sum until 1.0" loop overshoots and never hits its stop condition.
- A test asserting
expect(total).toBe(0.3)fails on a machine where the numbers are computed in a different order. - IDs or hashes built from float math differ between runs.
Fix 1: compare with a tolerance
Never use === on computed floats. Compare the distance instead:
const isEqual = (a, b) => Math.abs(a - b) < Number.EPSILON; Number.EPSILON is the gap between 1 and the next representable double. That is fine for values around 1, but it is too strict for big numbers, where the gap grows. If you compare money or large quantities, scale the tolerance:
const isClose = (a, b, rel = 1e-9) => Math.abs(a - b) <= rel * Math.max(Math.abs(a), Math.abs(b)); Fix 2: work in integers (cents, not dollars)
If the value is money, store cents. Integer arithmetic is exact up to 2^53, which is more than enough for any invoice:
const price = 1999; // 19.99 const shipping = 499; // 4.99 const total = (price + shipping) / 100; // 24.98 Convert at the edges (parsing input, formatting output) and keep everything integer in between.
Fix 3: use a decimal library
When the numbers must be decimal-accurate by definition (prices, tax, interest, invoices), use a decimal type instead of guessing with floats: decimal.js, big.js, or bignumber.js. They are slower, and that is fine, because they are only in the money path.
import Decimal from "decimal.js"; new Decimal("0.1").plus("0.2").toString(); // "0.3" Fix 4: round only for display
toFixed(2) is for showing a number to a human. It is not a fix. Rounding before comparison or before storing hides the error until someone sums a thousand rows and the totals disagree by a cent.
(0.1 + 0.2).toFixed(2) // "0.30" - display only The one rule
Floats are fine for measuring things (temperatures, distances, physics). They are wrong for counting money. Pick the representation from what the number means, not from what the language defaults to.
If you want the 60-second version of this with the machine-level picture, I made a short explainer here: https://www.youtube.com/watch?v=C6wp0GgISmw