What is a float — and why 0.1 + 0.2 doesn't equal 0.3

Open your browser console right now and type 0.1 + 0.2. You won't get 0.3 — you'll get 0.30000000000000004.
This isn't a JavaScript bug. Python, Java, C and the calculator inside your phone will all answer the same way — the phone just rounds before showing you. That's how nearly every computer on the planet does arithmetic, and it regularly burns first projects that touch money.
Computers count in binary — and 0.1 doesn't fit
Think of 1/3 in decimal: 0.3333… forever. You can't write it exactly, so you cut it off somewhere. We're used to that and don't find it strange.
In binary, where the computer lives, 0.1 behaves exactly the same way. So does 0.2. They can't be written exactly with a finite number of binary digits — you can only take the nearest available value, a hair off. Add two hairs together and the miss shows up at the seventeenth digit.
The name itself — floating point — describes the mechanism. The number is stored as significant digits and an exponent separately, so the point can "float". One field can hold both 0.0000001 and 10000000000000000000. The price of that range is precision.
A neat check: 0.5 + 0.25 gives exactly 0.75. Because a half and a quarter are powers of two, and those fit binary perfectly. It isn't that all fractions lie — only the ones that can't be expressed as powers of two.
Where it actually bites: money
Multiply 99.99 by 3. You get 299.96999999999997.
On screen you'll round it and never notice. But then you sum a thousand such rows in a report — and the total disagrees with the payment provider by a cent. That's brutal to track down later: every single row looks correct on its own.
Hence the rule worth learning once: never store money in a float.
- Store whole cents (or kopecks): not
99.99, but9999. Integers add and multiply exactly. - Divide by 100 only when showing it to a human.
- If your database does the arithmetic, use the
decimal/numerictype — it was invented for precisely this.
Look especially carefully at code that handles payments: price, discount, tax, total — four multiplications, four chances to grow a tail.
How to compare decimal numbers
Second rule: don't compare floats with equality.
// the landmine
if (total === 0.3) { ... } // fails when total = 0.1 + 0.2
// how to do it
if (Math.abs(total - 0.3) < 1e-9) { ... }
The idea is simple: you're not checking "are these equal", you're checking "are they close enough". You pick the threshold (called epsilon) to fit the task — 1e-9 covers ordinary calculations.
Equality checks like that are a classic in model-generated code: it looks perfectly logical and quietly fails one run in a hundred. It's one of those cases that explains why AI-written code has bugs — the model reproduces a familiar shape rather than thinking about binary representation.
And one more: don't accumulate decimals in a long loop when precision matters. A thousand additions means a thousand tiny misses, all leaning the same way.
Discounts and percentages: round once
A separate trap that every online shop on earth falls into. Say we apply a 15% discount and then tax:
// bad: rounding at every step
const afterDiscount = round2(price * 0.85);
const total = round2(afterDiscount * 1.2);
// good: calculate straight through, round at the very end
const total = round2(price * 0.85 * 1.2);
In the first version you rounded an intermediate result — and everything after it is computed from an inexact number. On one purchase the difference is pennies; on a monthly report it's visible. The rule: rounding is an output operation, not a calculation step. Full precision inside (or whole cents), one rounding at the very end.
Where a float is exactly right
So you don't overcorrect: the type isn't "bad", it's just built for measurements rather than counting.
- Coordinates, sizes, angles, speed — where the value is approximate anyway, because it was measured.
- Percentages, ratings, averages — the result gets rounded to one decimal regardless.
- Progress bars, animations, charts — an error in the sixteenth digit is invisible in pixels.
A simple test: if a human could recompute the result by hand and check it to the last digit (an invoice, a salary, stock on a shelf), you need integers. If the result is "about this much" by its very nature, a float is right at home.
What to take away
- Any float is an approximate value, always.
- Money, quantities, counters — integers.
- Comparison — with a tolerance, not
==. - Round for display (
toFixed(2)), but calculate on the original numbers, never the rounded ones.
So decimal maths always lies?
The error sits around the sixteenth decimal place. For graphics, physics, percentages and any measurement it simply doesn't matter. The problem only appears where errors accumulate — or where the last cent has to be exactly right.
Can whole numbers drift too?
Yes, if they're very large. In JavaScript, ordinary numbers are reliably exact up to roughly 9 × 10¹⁵ — beyond that the same approximations begin. For long database identifiers that's a real trap: an id can shift by one. Which is why such ids are usually sent as strings.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





