Why 0.1 + 0.2 is not 0.3 in JavaScript
Intermediate · 3 min read
Type 0.1 + 0.2 into JavaScript and you get 0.30000000000000004. Most people's first reaction is that JavaScript is broken. It isn't. Python, Java and C give the same answer, because they store decimals the same way. Once you see why, the result stops being a mystery, and you will know how to avoid it in prices and totals.
In this tutorial you will prove that 0.1 + 0.2 !== 0.3, look at the hidden digits, find decimals that do add up exactly, and see the safe way to handle money. You need to know what a number is. No advanced math is needed.
Why some fractions never end
Write one third as a decimal: 0.3333... The 3s go on forever. You have to stop somewhere, so the decimal you write down is slightly wrong.
Computers store numbers in binary, using only the digits 0 and 1. In binary, the fractions that end neatly are halves, quarters, eighths and so on: fractions whose bottom number is a power of two. One tenth isn't one of those. In binary, 0.1 goes on forever, just as one third does in decimal.
So JavaScript stores the nearest value it can fit, which is a tiny bit more than 0.1. The same happens with 0.2. Add two numbers that are each slightly off, and the error can become big enough to see.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
61 Why 0.1 + 0.2 is not 0.3
fairProve that 0.1 + 0.2 !== 0.3, show what the sum actually is, and find two decimals that do add up exactly.
Stuck? Three hints, smallest first
- 1. Numbers are stored in binary. One tenth in binary recurs, exactly as one third does in decimal.
- 2. Print the sum with enough decimal places to see the error:
toFixed(17). - 3. A fraction whose denominator is a power of two — a half, a quarter, an eighth — is stored exactly.
Step 1: prove it
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
The sum is a hair too big, so === says the two are different.
Step 2: see the hidden digits
When JavaScript prints a number, it shows the shortest decimal that identifies the stored value exactly. That is why 0.1 normally prints as 0.1. Ask for more decimal places with toFixed and the error shows up:
console.log((0.1).toFixed(20)); // 0.10000000000000000555
console.log((0.1 + 0.2).toFixed(17)); // 0.30000000000000004
Step 3: it isn't only addition
Subtraction and multiplication drift too, sometimes low and sometimes high:
console.log(0.3 - 0.1); // 0.19999999999999998
console.log(1.1 * 3); // 3.3000000000000003
console.log(0.07 * 100 === 7); // false
The last line is the bug in miniature: apply a 7% rate, compare it with a stored total, and they don't match.
Step 4: decimals that add up exactly
Halves and quarters are powers of two, so binary stores them exactly, and sums of them are exact too:
console.log(0.5 + 0.25); // 0.75
console.log(0.5 + 0.25 === 0.75); // true
Step 5: toFixed hides the error
You might reach for toFixed(2) to "fix" the sum:
console.log((0.1 + 0.2).toFixed(2)); // 0.30
That looks right, and it is fine for showing a number on screen. But it gives back text, and the stored value is still slightly off. It hides the error rather than fixing it.
Step 6: count money in whole units
Whole numbers are stored exactly, all the way up to 2 to the power of 53, which is 9,007,199,254,740,992. So the safe way to handle money is to count in the smallest unit: kobo instead of naira, cents instead of dollars.
console.log(10 + 20); // 30
console.log(10 + 20 === 30); // true
10 kobo plus 20 kobo is exactly 30 kobo. Divide by 100 only when you show the amount to a person.
The full solution
The answer panel prints the sum, its 17-place version, the drifting subtraction and multiplication, the exact half-plus-quarter and the kobo sum. The checks prove each claim, including that toFixed hides the error. Press Run to see the output.
console.log('0.1 + 0.2 =', 0.1 + 0.2);
console.log('to 17 places:', (0.1 + 0.2).toFixed(17));
console.log('0.3 - 0.1 =', 0.3 - 0.1);
console.log('1.1 * 3 =', 1.1 * 3);
console.log('0.5 + 0.25 =', 0.5 + 0.25, '<- exact: both are powers of two');
console.log('in kobo: 10 + 20 =', 10 + 20);
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the sum is not 0.3', 0.1 + 0.2 === 0.3, false);
test('it is a hair too big', 0.1 + 0.2, 0.30000000000000004);
test('subtraction misses low instead', 0.3 - 0.1, 0.19999999999999998);
test('and multiplication drifts too', 1.1 * 3, 3.3000000000000003);
test('halves and quarters are exact', 0.5 + 0.25 === 0.75, true);
test('0.07 * 100 is not 7', 0.07 * 100 === 7, false);
test('toFixed hides the error rather than fixing it', (0.1 + 0.2).toFixed(2), '0.30');
test('integers below 2**53 are exact, so minor units add up', 10 + 20 === 30, true);
This is not a JavaScript bug; it is IEEE 754, and Python, Java and C behave identically. A double stores a binary fraction, and one tenth in binary recurs forever, so 0.1 is really the nearest storable value slightly above it. Add two of those and the error is visible at the 17th place.
The practical consequences: never compare two computed decimals with === (exercise 62 shows the tolerance), and never hold money in naira-with-decimals. 0.07 * 100 !== 7 is that bug in miniature — a 7% rate applied to a price, then compared against a stored total that was calculated a different way. Integers are exact up to 2⁵³, so count in kobo.
Common mistakes
- Comparing computed decimals with
===. Two values that should be equal can differ in the last digit. Compare with a small tolerance instead. - Storing money as decimals. Sums of naira-with-kobo drift. Store whole kobo and format when you display.
- Thinking
toFixedfixes the value. It only rounds the text it gives back.
Try it yourself
- Add
0.1to a total ten times in a loop, then print the total and check whether it equals1. - Write
isClose(a, b)that returnstruewhenMath.abs(a - b)is below0.000001, and use it on0.1 + 0.2and0.3. - Add three prices of 19.99 in kobo (
1999) and print the total in naira with two decimals.