Skip to main content

JavaScript Numbers & Math

JavaScript Number Precision

Written by Published

Decimal arithmetic in JavaScript is not always exact, which is why 0.1 plus 0.2 is not 0.3.

This is not a JavaScript bug; it affects almost every language.

Numbers are stored in binary, and some decimals cannot be written exactly in binary.

Example

Example

javascript

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);

The output is 0.30000000000000004 then false.

The tiny error is real and it matters.

Why It Happens

Numbers are stored as binary fractions.

One tenth cannot be written exactly in binary, just as one third cannot in decimal.

The stored value is very slightly off, and errors add up.

Syntax

Syntax

javascript

Math.abs(a - b) < 0.0001

Compare decimals with a tolerance, not with equality.

Comparing Safely

Check that the difference is small enough rather than exactly zero.

Example

Example

javascript

const total = 0.1 + 0.2;

console.log(total === 0.3);
console.log(Math.abs(total - 0.3) < 0.0001);

The second check is the reliable one.

Rounding for Display

toFixed hides the error when showing the value.

Example

Example

javascript

const total = 0.1 + 0.2;

console.log(total.toFixed(2));
console.log(Number(total.toFixed(2)) === 0.3);

Rounding produces the value a person expects.

Working in Whole Numbers

For money, store pence rather than pounds.

Whole numbers are exact, so nothing drifts.

Example

Example

javascript

const a = 10;
const b = 20;

console.log((a + b) / 100);
console.log((a + b) / 100 === 0.3);

The output is 0.3 then true.

Errors Accumulate

Adding the same value many times makes the drift visible.

Example

Example

javascript

let total = 0;

for (let i = 0; i < 10; i++) {
  total += 0.1;
}

console.log(total === 1);
console.log(total.toFixed(2));

Ten additions of a tenth do not give exactly one.

Whole Numbers Are Exact

The problem only affects fractions, up to a very large size.

Example

Example

javascript

console.log(1 + 2 === 3);
console.log(1000000 + 1 === 1000001);

Integer arithmetic is reliable until extremely large values.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Number Precision</title>
</head>
<body>

  <h1>Number Precision</h1>

  <p id="out"></p>

  <script>
    const total = 0.1 + 0.2;

    document.getElementById("out").innerHTML =
      "0.1 + 0.2 = " + total +
      "<br>Equals 0.3: " + (total === 0.3) +
      "<br>Rounded: " + total.toFixed(2) +
      "<br>Close enough: " + (Math.abs(total - 0.3) < 0.0001) +
      "<br>In pence: " + ((10 + 20) / 100);
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try other decimals:

Test 0.2 + 0.4 and see whether it is exact.

Important Points

  • Decimal arithmetic is not always exact.
  • 0.1 + 0.2 is not exactly 0.3.
  • Compare decimals with a tolerance.
  • Round with toFixed for display.
  • Store money as whole pence to stay exact.

Conclusion

This is the most surprising thing about numbers in JavaScript.

It is a property of binary floating point, not a language flaw.

Working in whole numbers is the standard fix for money.