August 3, 2026 · Varun Sharma

Why 0.1 + 0.2 Doesn't Equal 0.3 (And Why Every Developer Eventually Gets Burned By It)

If you've been coding for more than a few months, you've probably typed something like this into a console just to sanity-check basic math — and watched your screen betray you.

0.1 + 0.2 = 0.30000000000000004

Not a typo. Not a joke. This happens in JavaScript, Python, PHP, Java, C, Go — basically every mainstream language that uses standard floating-point numbers. It's one of the most-searched programming "bugs" on the internet, and yet almost nobody explains it in a way that actually helps you avoid it in production.

Let's fix that.

It's Not a Bug. It's Binary.

Computers store decimal numbers in binary floating-point format (IEEE 754). The problem is that most "simple" decimal fractions — like 0.1 or 0.2 — can't be represented exactly in binary, the same way 1/3 can't be written exactly in decimal (0.333333...).

So when you write 0.1, the computer actually stores something incredibly close to 0.1, but not quite. Add two of these "almost right" numbers together, and the tiny rounding errors surface as a visible discrepancy.

This isn't a JavaScript problem, a PHP problem, or a Python problem. It's a hardware and standards problem that every language built on IEEE 754 inherits.

Seeing It in the Wild

JavaScript

javascript

console.log(0.1 + 0.2);        // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false

Python

python

print(0.1 + 0.2)          # 0.30000000000000004
print(0.1 + 0.2 == 0.3)   # False

PHP

php

echo 0.1 + 0.2;              // 0.3 (PHP rounds display output)
var_dump(0.1 + 0.2 == 0.3);  // bool(false)

Notice PHP is sneaky here — it looks fine when you echo it because PHP rounds the displayed string, but the underlying comparison still fails. That's arguably worse, because it hides the bug until it silently corrupts a calculation somewhere downstream — like a shopping cart total that's off by a fraction of a cent, multiplied across thousands of transactions.

Why This Actually Matters

This isn't just a fun party trick for Stack Overflow. It causes real bugs:

  • E-commerce: prices, taxes, and discounts drifting by fractions of a cent, which compounds at scale

  • Finance: interest calculations, currency conversion, and ledger balances that don't reconcile

  • Comparisons: if (total === 100.30) silently failing forever because total is actually 100.30000000000001

  • Loops: for (let i = 0; i != 1; i += 0.1) that never terminates because i never exactly equals 1

The scary part is that this bug doesn't crash your app. It just quietly produces wrong numbers, and wrong numbers are often worse than a crash — because nobody notices until a customer, an accountant, or an auditor does.

How to Actually Fix It

1. Never compare floats with == or ===

Use an epsilon (a tiny tolerance) instead.

javascript

function nearlyEqual(a, b, epsilon = Number.EPSILON) {
  return Math.abs(a - b) < epsilon;
}

python

import math
math.isclose(0.1 + 0.2, 0.3)  # True

php

abs((0.1 + 0.2) - 0.3) < PHP_FLOAT_EPSILON; // true

2. Work in integers, not decimals, for money

The single best fix for financial data: store amounts as cents (integers), not dollars (floats), and only convert to a decimal for display.

javascript

// Store $19.99 as 1999 cents
const totalCents = 1999 + 250; // add $2.50 in cents
const totalDollars = (totalCents / 100).toFixed(2);

3. Use dedicated decimal libraries for serious precision work

  • JavaScript: decimal.js or big.js

  • Python: the built-in decimal module

  • PHP: the BCMath extension (bcadd, bcsub, etc.)

python

from decimal import Decimal
Decimal('0.1') + Decimal('0.2')  # Decimal('0.3') — exact

php

bcadd('0.1', '0.2', 2); // "0.30" — exact

4. Round only at the boundary, not mid-calculation

Rounding intermediate steps compounds errors. Do all your math at full precision, and round once, right before displaying or storing the final value.

The Takeaway

Floating-point imprecision isn't a flaw in your code — it's a fundamental limitation of how computers represent decimal numbers in binary. You can't "fix" binary. You can only design around it.

The rule of thumb that will save you (and your users) a lot of pain:

Never trust float equality. If money or precision matters, use integers or a decimal library — not raw floats.

Once you internalize that, 0.1 + 0.2 !== 0.3 stops being a scary gotcha and becomes just... how computers work.


Have you been bitten by this bug in production? What language caught you off guard? Drop it in the comments — misery loves company.