الانتقال إلى المحتوى
FrontendJavaScript· 6 دقائق للقراءة

0.1 + 0.2 !== 0.3 Isn't a JavaScript Bug: Floating-Point Numbers Explained

Binary numbers can't hold 0.1 exactly. toFixed hides that for display; integers fix it for money.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • ES2025
محتويات المقال

A food delivery app gives 0.10 EGP cashback per order. After a customer's tenth order, the wallet screen shows "1.00 EGP", but the "Redeem 1 EGP" button stays disabled. The check is balance >= 1, and logging balance prints 0.9999999999999999. The bug report says "JavaScript can't add", and the fix in review is Number(balance.toFixed(2)) >= 1. It passes this test, and it leaves the real problem in place.

The common misconception

The belief: JavaScript's number type is broken, 0.1 + 0.2 should simply be 0.3, and toFixed(2) is the standard way to clean up results, especially for money.

It's easy to see why. JavaScript has one Number type for integers and fractions alike, so developers meet floating-point math on day one, while in many other languages they would reach for an integer or decimal type first. And toFixed(2) makes the screen look right, so it feels like a fix. The true part: JavaScript has no built-in decimal type, so you do need a strategy for money. But 0.30000000000000004 is correct floating-point arithmetic, and toFixed only changes how a number is printed.

What actually happens

Numbers are binary fractions

The spec defines every Number value as an , the 64-bit binary format that MDN compares to double in Java or C#. It has 1 sign bit, 11 exponent bits and 52 bits of fraction, which gives about 15 to 17 significant decimal digits. Integers are exact only up to 2^53 − 1, which is Number.MAX_SAFE_INTEGER.

0.1 has no exact binary form

A decimal system can't write 1/3 exactly, and a binary system can't write 0.1 exactly. MDN's Number.EPSILON page makes exactly this point: any format with a finite number of bits must leave most numbers out. So the literal 0.1 is stored as the nearest double, which is slightly above 0.1. The same happens to 0.2. Their sum is rounded to the nearest double again, and it lands on the double just above 0.3, while the literal 0.3 is stored just below it.

Printing hides most of this. JavaScript converts a number to the shortest decimal string that identifies the stored value, so 0.1 prints as 0.1 even though it isn't exactly 0.1. The sum needs 17 digits to be told apart from 0.3, so you see them all:

JavaScript floats.js
console.log(0.1 + 0.2); // 0.30000000000000004console.log((0.1).toFixed(20)); // 0.10000000000000000555console.log(0.1 + 0.2 === 0.3); // falseconsole.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true, near 1console.log(1000.1 + 1000.2); // 2000.3000000000002console.log(2 ** 53 + 1); // 9007199254740992: integers stop being exact

toFixed formats, it doesn't fix

toFixed(2) returns a string with the number rounded to two decimals. As a money fix it fails in three ways. It's a string, so + concatenates instead of adding. It rounds the stored binary value, not the decimal you typed: MDN shows (2.55).toFixed(1) returning "2.5", because the closest double to 2.55 is slightly below it. And converting the string back with Number() gives you a double again, so the next operation can drift again. It's the right tool at the very end, when you show a number to a person.

Tolerances are for measurements

For values that are approximate by nature, like distances, sensor readings or animation progress, compare with a tolerance: Math.abs(a - b) < tolerance. MDN warns that Number.EPSILON only works as that tolerance for numbers around 1. Near 2000, the error in the line above is about 10^-13, far bigger than EPSILON, so choose a tolerance from the magnitude and the precision of your data. Money isn't approximate, though. It needs exact arithmetic.

Fixing the wallet

Counting in piastres, the of the Egyptian pound, turns every amount into an integer, and integer addition is exact up to Number.MAX_SAFE_INTEGER, which is over 90 trillion pounds in piastres. The division by 100 happens once, for display:

JavaScript wallet.js
let balance = 0;for (let order = 1; order <= 10; order++) {  balance += 0.1; // 0.10 EGP cashback per order}console.log(balance); // 0.9999999999999999console.log(balance >= 1); // false: "Redeem 1 EGP" stays disabledconsole.log(balance.toFixed(2)); // 1.00, but it's a stringconsole.log((0.1).toFixed(2) + (0.2).toFixed(2)); // 0.100.20console.log((2.55).toFixed(1)); // 2.5: 2.55 is stored slightly below
ApproachGood forWatch out for
=== on computed decimalsNothing that went through arithmetic0.1 + 0.2 === 0.3 is false
toFixed(2)Showing a number with fixed digitsReturns a string; rounds the binary value, so 2.55 gives "2.5"
A tolerance, Math.abs(a - b) < tolMeasurements, geometry, animationScale it to the magnitude; EPSILON only fits values near 1
Integer minor unitsMoney sums, comparisons, thresholdsStay below MAX_SAFE_INTEGER, or switch to BigInt
Intl.NumberFormat with a currencyDisplaying amounts to usersIt rounds for display only; stored errors stay stored

How to apply it in your project

  1. Store money as integersKeep amounts in piastres or cents in state, storage and API payloads. Convert a two-decimal input once with Math.round(Number(text) * 100).
  2. Stop comparing computed decimals with ===Compare integers, or use a tolerance that fits the magnitude and precision of the values.
  3. Format only at the edgeUse Intl.NumberFormat, or toFixed for simple cases, right where a number becomes text on screen, never in the middle of a calculation.
  4. Own your rounding rulePut tax, discount and split rounding in one tested helper, so every screen and every invoice rounds the same way.

Key takeaways

  • JavaScript numbers are IEEE 754 doubles, the same format as double in Java and C#, so 0.1 + 0.2 isn't a JavaScript bug.
  • Most decimal fractions, like 0.1, have no exact binary form, so arithmetic on them carries tiny rounding errors.
  • toFixed formats for display: it returns a string and rounds the stored binary value, so it isn't a money fix.
  • For money, compute in integer minor units and format with Intl.NumberFormat at the end.
  • For measurements, compare with a tolerance that matches the magnitude and precision of your data.

📚 المصادر

  1. Number — MDN
  2. Number.EPSILON — MDN
  3. Number.prototype.toFixed() — MDN
  4. Intl.NumberFormat() constructor — MDN
  5. The Number Type — ECMAScript Language Specification

التعليقات (0)