Basics
Floating-point precision: 0.1 + 0.2
Fractions are stored approximately in binary, so you can't compare them with ===, and money is best kept in integers.
Why it exists
If you don't know about this, a program goes wrong in the strangest places: the cart shows "Total: $0.30000000000000004", a total === 0.3 check never fires, a 10% discount applied three times "loses" a cent, and the accounting report doesn't match the bank statement. It isn't a JavaScript bug, it's the price of speed: the processor works with binary fractions. Once you know the rules, the mistakes are easy to avoid — compare with a tolerance and keep money in whole cents.
Where it's used
- Online shops and tills: prices are kept in cents (
4990, not49.9) and divided by 100 only for display. - Payment APIs: in Stripe, for example, an amount is always an integer in the currency's smallest unit.
- Games and animation: coordinates are compared "close enough" —
Math.abs(x - target) < 0.001— otherwise an object never exactly "arrives" at its target. - Tests: checks on fractional results use a tolerance (Jest and Vitest have
toBeCloseTofor that).
Syntax
Math.abs(a - b) < 1e-9 // "close enough"
Number.EPSILON // about 2.2e-16
Number.MAX_SAFE_INTEGER // 2 ** 53 - 1Examples
0.1 + 0.2; // → 0.30000000000000004
0.1 + 0.2 === 0.3; // → false
Math.abs(0.1 + 0.2 - 0.3) < 1e-9; // → true(0.1).toFixed(20); // → "0.10000000000000000555"
0.5 + 0.25 === 0.75; // → true — powers of two are stored exactly0.1const bread = 499; // $4.99 in cents
const milk = 289;
const total = bread + milk;
total; // → 788
(total / 100).toFixed(2); // → "7.88"function almostEqual(a, b, tolerance = 1e-9) {
return Math.abs(a - b) < tolerance;
}
almostEqual(0.1 + 0.2, 0.3); // → true
almostEqual(0.1, 0.2); // → falseResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Every number in JavaScript takes 64 bits under the IEEE 754 standard: 1 bit for the sign, 11 bits for the exponent (where the point goes), 52 bits for the digits. That's roughly 15–17 significant decimal digits.
In binary, only fractions whose denominator is a power of two are exact: 0.5, 0.25, 0.125. But 0.1 in binary never ends (0.000110011…), just like 1/3 = 0.333… in decimal. The tail is cut off, and memory holds the nearest representable number — a hair above 0.1. When you add such numbers the errors pile up, and the sum lands on a neighbour of 0.3.
This is a property of the processor, not the language: Python, Java, C and Go give the same result for 0.1 + 0.2.
How to live with it:
- comparing — "close enough":
Math.abs(a - b) < tolerance.Number.EPSILON(about2.2e-16) is the gap between1and the next number; it works for numbers near one, bigger numbers need a bigger tolerance; - money — integers in the smallest unit (cents): integers are exact up to
Number.MAX_SAFE_INTEGER(2 ** 53 - 1); - display — toFixed or
Intl.NumberFormat, at the very end.
Traps
How it differs from…
- Rounding:
Math.round,floor,ceil,toFixed Rounding hides the
0.30000000000000004tail when displaying, but it doesn't make the maths exact. Round once, at the end, and calculate in integers.
When to pick something else
Don't bother with tolerances and cents where there are no fractions: integers — counters, indexes, item quantities — add up and compare with === exactly. And don't invent your own "exact" arithmetic for accounting with percentages and exchange rates — there are decimal arithmetic libraries for that (decimal.js, for example).