Floating-point surprises like 4.35 * 100 in JavaScript
Intermediate · 4 min read
0.1 + 0.2 is the famous one, but it isn't alone. Multiply a price of 4.35 by 100 to get kobo and you get 434.99999999999994. Take 7% of 100 and you get 7.000000000000001. These small errors turn into real bugs as soon as you round down, count, or compare.
In this tutorial you will print the exact results of a few everyday sums, then work out why Math.floor((0.1 + 0.7) * 10) gives 7 when you expected 8. You need to know Math.floor and Math.round, and it helps to have done the 0.1 + 0.2 exercise.
A quick reminder
JavaScript stores numbers in binary with a fixed number of digits. Most decimals, like 0.1, 0.7 and 4.35, can't be stored exactly, so each is kept as the closest value available. That closest value can be a hair above or a hair below the number you typed. Do some math with it and the tiny error can show up in the result.
The rule for this exercise
Print first, then write down what you see. The real value of each sum is whatever JavaScript produces, not what school math says it should be. That is the only way to know which way an error went.
Here is the exercise. Try it yourself first if you like, then read on and we will go through it together.
13 A gallery of floating-point surprises
fairAssert the exact value of 0.1 * 3, 0.3 - 0.1, 4.35 * 100 and 0.07 * 100. Then find why Math.floor((0.1 + 0.7) * 10) is 7.
Stuck? Three hints, smallest first
- 1. Print each result before asserting it. The exact double is what you must write down.
- 2. Multiplying a price by 100 to get minor units is the single most common victim.
- 3.
Math.flooron a value that is a hair under 8 gives 7, and there is no warning.
Step 1: multiply and subtract
console.log(0.1 * 3); // 0.30000000000000004
console.log(0.3 - 0.1); // 0.19999999999999998
console.log(1.1 * 3); // 3.3000000000000003
The first goes over. The second comes in under. The third is visibly wrong in the last digit. There is no rule you can memorize for which way it goes.
Step 2: prices times 100
Shops often convert naira to kobo by multiplying by 100:
console.log(4.35 * 100); // 434.99999999999994
console.log(0.07 * 100); // 7.000000000000001
4.35 is stored slightly under 4.35, so the result is slightly under 435. 0.07 is stored slightly over, so 7% of 100 is slightly over 7. One comes out low, the other high.
Step 3: rounding down a value that is a hair low
Now the trap. If you turn 434.99999999999994 into a whole number by chopping off the fraction, you lose a whole kobo:
console.log(Math.floor(4.35 * 100)); // 434
console.log(Math.round(4.35 * 100)); // 435
Math.floor goes down, so it gives 434. Math.round goes to the nearest, so it gives 435, which is right. The true value is never more than a tiny bit away, so the nearest whole number is always the one you meant.
Step 4: the (0.1 + 0.7) * 10 puzzle
This one looks like plain whole-number math, which is why it's famous:
console.log(0.1 + 0.7); // 0.7999999999999999
console.log((0.1 + 0.7) * 10); // 7.999999999999999
console.log(Math.floor((0.1 + 0.7) * 10)); // 7
console.log(Math.round((0.1 + 0.7) * 10)); // 8
Follow it one line at a time. 0.1 + 0.7 lands just under 0.8. Times 10, that's just under 8. Math.floor goes down to the next whole number below, which is 7. No error, no warning, just an answer that is off by one.
Math.round gives 8, because 7.999999999999999 is nearest to 8.
Step 5: why "add a tiny bit" doesn't fix it
A tempting fix is to add a small amount before flooring, like Math.floor(x + 0.0001). It works for 434.99999999999994. But remember 0.07 * 100 came out over 7. An error that can go either way can't be fixed by always nudging one way. You would just move the bug to a different input.
The real fixes are:
- Round, don't floor, whenever you turn a decimal quantity into a whole count.
- Keep whole numbers from the start. Store kobo, not naira, so there is nothing to convert (exercise 15).
The full solution
The answer panel prints every sum from this lesson, then floors and rounds (0.1 + 0.7) * 10 side by side. The checks underneath assert the exact value of each result, so you can see each error's direction. Press Run to see the output.
console.log('0.1 * 3 :', 0.1 * 3);
console.log('0.3 - 0.1 :', 0.3 - 0.1);
console.log('1.1 * 3 :', 1.1 * 3);
console.log('4.35 * 100 :', 4.35 * 100, '<- kobo, apparently');
console.log('0.07 * 100 :', 0.07 * 100, '<- 7% of 100');
console.log('0.1 + 0.7 :', 0.1 + 0.7);
console.log('times 10 :', (0.1 + 0.7) * 10);
console.log('floored :', Math.floor((0.1 + 0.7) * 10), '<- the classic off-by-one');
console.log('rounded instead:', Math.round((0.1 + 0.7) * 10));
Output
9 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 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a tenth times three overshoots', 0.1 * 3, 0.30000000000000004);
test('and this subtraction undershoots', 0.3 - 0.1, 0.19999999999999998);
test('1.1 * 3 is visibly wrong', 1.1 * 3, 3.3000000000000003);
test('a price times 100 is not the kobo count you wanted', 4.35 * 100, 434.99999999999994);
test('so it needs rounding before being treated as an integer', Math.round(4.35 * 100), 435);
test('7 percent of 100 is not 7', 0.07 * 100, 7.000000000000001);
test('0.1 + 0.7 loses the last digit', 0.1 + 0.7, 0.7999999999999999);
test('so flooring the tenfold gives 7, not 8', Math.floor((0.1 + 0.7) * 10), 7);
test('while rounding it gives 8', Math.round((0.1 + 0.7) * 10), 8);
The direction of the error is not predictable. 4.35 * 100 comes out low and 0.07 * 100 comes out high, so "add a tiny amount before flooring" is not a fix, it is a different bug. Math.round is the right tool whenever you are converting a decimal quantity to an integer count, because the true value is always within half a unit.
Math.floor((0.1 + 0.7) * 10) is the canonical example because it looks like integer arithmetic. Anywhere you compute a bucket index, a page number or a count of minor units by flooring a product, this is waiting. Round, or keep integers from the start.
Common mistakes
- Using
Math.floororMath.truncto turn naira into kobo.Math.floor(4.35 * 100)is 434. UseMath.round. - Writing down the value you expect instead of the one you get.
0.07 * 100is 7.000000000000001. Print it before you test it. - Assuming the error always goes the same way. 4.35 * 100 is low and 0.07 * 100 is high.
Try it yourself
- Print
1.15 * 100and9.95 * 100. Which way does each error go? - A score of 0.57 should be 57 percent. Print
Math.floor(0.57 * 100). What goes wrong, and how would you fix it? - Write
toKobo(naira)that always gives the right whole number of kobo, and test it with 4.35 and 0.07.