Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Floating-point numbers in JavaScript

Understand JavaScript floating-point numbers: 64-bit doubles, why 0.1 + 0.2 is not 0.3, safe integers, comparing with EPSILON, rounding and money.

Every JavaScript number is a 64-bit binary floating-point value (the IEEE 754 "double" format). It has 53 bits of precision for the digits and the rest for the exponent and sign. This one format covers integers, decimals, huge numbers around 1e308 and tiny ones around 5e-324.

The catch is that computers store numbers in base 2, and many everyday decimals cannot be written exactly in base 2. Just as one third is 0.3333... in decimal, one tenth is a repeating fraction in binary. The stored value is the nearest double, and small errors show up in the results. This is not a JavaScript bug: Python, Java, C and many other languages behave the same way.

The classic surprise

JS Not quite what you typed

Press Run (or Ctrl + Enter) to see the console output here.

No output.

0.1, 0.2 and 0.3 are all stored as the nearest representable double. Adding the first two lands on a neighbouring double that is not the one stored for 0.3. Numbers made from powers of two, such as 0.5 and 0.25, are exact.

Comparing safely

Do not compare computed decimals with ===. Compare their difference with a small tolerance. Number.EPSILON is the gap between 1 and the next double, so it is a good tolerance for values near 1. For other sizes, scale it or pick a tolerance that fits your data.

JS Comparing with a tolerance

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Limits to know

  • Safe integers. Whole numbers are exact only up to Number.MAX_SAFE_INTEGER (2^53 - 1, which is 9,007,199,254,740,991). Beyond that, neighbouring integers collapse into the same double. Use BigInt for exact large integers.
  • Largest and smallest values. Number.MAX_VALUE is about 1.8e308; anything larger becomes Infinity. The smallest positive value is Number.MIN_VALUE, about 5e-324; smaller results underflow to 0.
  • Special values. NaN marks an invalid result, and Infinity and -Infinity mark overflow or division by zero. 0 and -0 are both zero, but Object.is(0, -0) is false.
  • Precision is relative. At a magnitude of one quadrillion, the gap between doubles is already larger than 0.1, so adding small decimals to huge values may do nothing.

Practical advice

  • Money: store amounts as integer cents (or the smallest unit), do arithmetic on integers, and format at the end with toLocaleString(). Avoid summing floating-point prices.
  • Display: use toFixed() or Intl formatting for output. Remember toFixed() returns a string.
  • Rounding to decimals: Math.round(x * 100) / 100 is common but can be off for values like 1.005. Prefer integer arithmetic where the exact result matters.
  • Accumulated error: summing many values can grow the error. Math.sumPrecise(), where supported, sums an iterable more accurately.
  • Need exact decimals? Use a decimal library, or integer scaling. There is no built-in decimal type in the language.

Browser support

This behavior is part of the language and identical in every browser and runtime. Number.EPSILON and the safe-integer constants are available in every current engine.