Emmanuel Akinfulubi

JavaScriptMath

Floating-point precision: 0.1 + 0.2 in JavaScript

Intermediate · 4 min read

Type 0.1 + 0.2 into JavaScript and you get 0.30000000000000004. Almost every developer meets this at some point, usually when a shopping cart shows ₦0.30000000000000004 or a test that should pass fails.

Nothing is broken. In this tutorial you will show that 0.1 + 0.2 === 0.3 is false, look at what the sum really is, understand why in plain words, and find decimals that do add up exactly. You only need to know console.log and ===.

How JavaScript stores decimals

JavaScript stores every number in a format called a double (short for double-precision floating point, defined by a standard named IEEE-754). A double holds the number in binary, base 2, with a fixed number of digits: 53 binary digits of precision.

Here is the catch. In our everyday base 10, a third can't be written exactly: 0.3333... goes on forever, so you stop somewhere and accept a tiny error. Binary has the same problem with different numbers. A tenth in binary is 0.000110011001100... repeating forever. JavaScript has to cut it off, so 0.1 is stored as the closest double, which is a tiny bit more than a tenth.

Fractions made from halves are different. A half, a quarter and an eighth are short, exact numbers in binary, just as a tenth is exact in base 10.

Here is the exercise. Try it yourself first if you like, then read on and we will work through it together.

11 Why 0.1 + 0.2 is not 0.3

fair

Show that 0.1 + 0.2 === 0.3 is false, print what the sum really is, and find two decimal fractions that do add up exactly.

Stuck? Three hints, smallest first
  1. 1. A double stores a binary fraction. A tenth is 0.0001100110011... in binary, repeating forever.
  2. 2. toFixed(20) shows more of the stored value than the default printing does.
  3. 3. A fraction whose denominator is a power of two — a half, a quarter, an eighth — is stored exactly.

Step 1: the comparison everyone tries

Step 1

console.log(0.1 + 0.2);         // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false

The sum is a hair above three tenths, so === says no. === checks that two numbers are exactly the same, down to the last binary digit.

Step 2: measure the error

Subtract the number you expected from the number you got:

Step 2

console.log(0.1 + 0.2 - 0.3); // 5.551115123125783e-17

e-17 means "times 10 to the minus 17th", so this is 0.0000000000000000555... It is tiny, but it isn't zero, and === only accepts zero difference.

Step 3: look inside 0.1

console.log shows the shortest text that reads back as the same double, which hides the error. toFixed(20) asks for 20 decimal places and shows more of the stored value:

Step 3

console.log((0.1).toFixed(20)); // 0.10000000000000000555
console.log((0.5).toFixed(20)); // 0.50000000000000000000

0.1 is stored slightly too big. 0.5 is a half, so it is stored exactly, and every digit after it is zero. When you add two values that are each slightly too big, the errors add up, and the result lands on a different double from the one 0.3 is stored as.

Step 4: decimals that do add up

Halves, quarters and eighths are exact, so sums made from them are exact too:

Step 4

console.log(0.5 + 0.25 === 0.75); // true
console.log(0.1 * 10 === 1);      // true

The second line works because rounding happens to land exactly on 1. Whole numbers are always exact (up to a very large limit you will meet in exercise 18), so values that come out whole are safe.

Step 5: the order of adding matters

In school math, a + b + c gives the same answer in any order. With doubles, every addition rounds, so the order can change the result:

Step 5

console.log(0.1 + 0.2 + 0.3); // 0.6000000000000001
console.log(0.3 + 0.2 + 0.1); // 0.6

Same three numbers, two different totals. This is why a report and its subtotal can disagree by a tiny amount with no bug anywhere in the code.

Step 6: rounding hides it

When you only need two decimal places, round the result:

Step 6

console.log(Math.round((0.1 + 0.2) * 100) / 100); // 0.3

Multiplying by 100 gives 30.000000000000004, rounding gives 30, and dividing by 100 gives the double that prints as 0.3. The next exercises cover the other ways out: comparing with a tolerance, and keeping money as whole numbers.

The full solution

The answer panel prints the sum, the failed comparison, the error, and the stored values of 0.1 and 0.5. It also shows that adding in a different order changes the total. The checks underneath confirm every exact value. Press Run to see the output.

The answer

console.log('0.1 + 0.2      :', 0.1 + 0.2);
console.log('=== 0.3        :', 0.1 + 0.2 === 0.3);
console.log('the difference :', 0.1 + 0.2 - 0.3);
console.log('0.1 really is  :', (0.1).toFixed(20));
console.log('0.5 really is  :', (0.5).toFixed(20), '<- exact, it is a power of two');
console.log('left to right  :', 0.1 + 0.2 + 0.3, 'against', 0.3 + 0.2 + 0.1);

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.

8 checks

test('the comparison everyone tries first is false', 0.1 + 0.2 === 0.3, false);
test('the sum is a hair over three tenths', 0.1 + 0.2, 0.30000000000000004);
test('the error is about 5.5e-17', 0.1 + 0.2 - 0.3, 5.551115123125783e-17);
test('0.1 is not exactly a tenth to begin with', (0.1).toFixed(20), '0.10000000000000000555');
test('so addition is not even associative here', [0.1 + 0.2 + 0.3, 0.3 + 0.2 + 0.1], [0.6000000000000001, 0.6]);
test('halves and quarters are exact, being powers of two', 0.5 + 0.25 === 0.75, true);
test('and integers below 2 ** 53 are exact as well', 0.1 * 10 === 1, true);
test('rounding the result to two places hides it', Math.round((0.1 + 0.2) * 100) / 100, 0.3);

Nothing is broken. A double has 53 bits of mantissa, one tenth in binary repeats forever, so 0.1 is stored as the nearest available double — a value slightly above a tenth. Two of those, added, land above three tenths, and 0.3 is a different nearest-double. Every language with IEEE-754 doubles does this; JavaScript is only unusual in having no separate integer type to hide behind.

The associativity assertion is the practical one. 0.1 + 0.2 + 0.3 and 0.3 + 0.2 + 0.1 differ, so summing the same figures in a different order can give a different total — which is why a report and its subtotal can disagree by a penny with no bug anywhere. Exercises 12 to 15 cover the three ways out: tolerance, integers, and compensated summation.

Common mistakes

  • Comparing decimal results with ===. 0.1 + 0.2 === 0.3 is false. Compare with a small tolerance (exercise 12) or round first.
  • Thinking this is a JavaScript bug. Python, Java, C and every other language that uses doubles store the same slightly-too-big sum.
  • Fixing it with toFixed and carrying on. toFixed returns a string. Round to a number, or keep money as whole numbers of kobo.

Try it yourself

  1. Check whether 0.1 + 0.7 === 0.8. Print the sum to see what it really is.
  2. Use toFixed(20) on 0.25, 0.2 and 0.375. Which are exact, and why?
  3. Add 0.1 to a running total ten times in a loop. Is the total exactly 1?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.