← Back to JavaScript, page 2

javascript-0029 · Easy fresher

Why does 0.1 + 0.2 not equal 0.3 in JavaScript, and how do you handle decimals safely?

JavaScript numbers are 64-bit binary floating-point values, and many decimal fractions such as 0.1 cannot be stored exactly in binary. The tiny stored errors show up in sums like 0.1 + 0.2. Compare with a tolerance, or avoid the problem by working in whole units, such as paise or cents, and use BigInt for integers beyond the safe range.

Full answer

All numbers in JavaScript, except BigInt, use the IEEE 754 double-precision format. It stores a value in binary. Just as one third cannot be written exactly in decimal (0.3333...), one tenth cannot be written exactly in binary. The stored value for 0.1 is slightly off, and so is the one for 0.2. Adding them gives 0.30000000000000004, which is a different double than the one stored for 0.3.

This is not a JavaScript bug. Python, Java, C, and most other languages give the same result for doubles.

The safe approaches depend on the job:

  • For comparisons, test whether the difference is smaller than a tolerance, such as Math.abs(a - b) < Number.EPSILON, scaled if the numbers are large.
  • For money, store whole minor units (paise or cents) as integers and convert only for display.
  • For display, toFixed(2) returns a string rounded to two decimals; remember it returns text and that rounding is applied to the binary value.
  • For very large whole numbers, integers are exact only up to Number.MAX_SAFE_INTEGER (2^53 - 1). Beyond that, use BigInt, written with an n suffix, which cannot be mixed with normal numbers in arithmetic.
  • For precise decimal math, use a decimal library.

Number.isInteger and Number.isSafeInteger help validate input.

javascript
console.log(0.1 + 0.2, 0.1 + 0.2 === 0.3);
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);

const priceInPaise = 1999;
const quantity = 3;
const totalInPaise = priceInPaise * quantity;
console.log(`Total: Rs ${(totalInPaise / 100).toFixed(2)}`);

console.log(typeof (0.5).toFixed(2), (1.005).toFixed(2), (1.255).toFixed(2));

console.log(Number.MAX_SAFE_INTEGER, 9007199254740992 === 9007199254740993);
console.log(9007199254740993n, 2n ** 64n);
try {
  console.log(1n + 1);
} catch (error) {
  console.log(error.name);
}
console.log(Number.isInteger(5.0), Number.isSafeInteger(2 ** 53));

Run with Node.js v24.22.1 as js-29.js; two runs gave identical output. Output: 0.30000000000000004 false true Total: Rs 59.97 string 1.00 1.25 9007199254740991 true 9007199254740993n 18446744073709551616n TypeError true false

Key points

  • Numbers are 64-bit binary doubles; many decimals cannot be stored exactly.
  • 0.1 + 0.2 is 0.30000000000000004 in many languages, not only in JavaScript.
  • Compare with a tolerance, not with ===.
  • Use integer minor units for money.
  • Integers are safe up to 2^53 - 1; use BigInt beyond that.

Common mistakes

  • Comparing computed decimals with ===.
  • Storing money as floating-point rupees or dollars.
  • Treating toFixed as returning a number, or expecting it to round like decimal arithmetic on 1.005.
  • Using Number for IDs larger than 2^53 and losing digits, for example 64-bit database IDs from an API.

Follow-ups

  • Why is Number.EPSILON a poor tolerance for large values, and what would you use instead?
  • How do you send a BigInt in JSON?
  • What does Math.round(2.5) return compared with Math.round(-2.5)?

Tips

  • State clearly that this is a property of binary floating point, not a JavaScript bug.
  • Offer the money rule (integer minor units) because it shows practical knowledge.
  • Mention MAX_SAFE_INTEGER and BigInt to extend the answer.