September 15, 2026 · Varun Sharma
Why 0.1 + 0.2 Doesn't Equal 0.3 (And Why Every Developer Should Know This)
Ever wondered why 0.1 + 0.2 sometimes gives you 0.30000000000000004 instead of 0.3? The reason is how computers represent decimal numbers.
You might expect this:
0.1 + 0.2 = 0.3But try it in JavaScript:
console.log(0.1 + 0.2);You may get:
0.30000000000000004At first, this looks like a bug.
It's not.
Why Does This Happen?
Computers store numbers using binary, which uses only:
0 and 1Some decimal numbers can be represented exactly in binary.
Others cannot.
The numbers 0.1 and 0.2 are examples of decimal values that cannot be represented exactly using the standard binary floating-point format.
So the computer stores very close approximations.
When those approximations are added together, the result can be:
0.30000000000000004The difference is extremely small, but it's real.
A Simple Example
Think of it like trying to represent 1/3 using only two decimal places.
You might write:
0.33But:
0.33 ≠ 0.333333...The stored value is only an approximation.
Floating-point numbers have a similar problem, except the approximation happens in binary.
Why Should Developers Care?
For most everyday calculations, the difference is insignificant.
But it can become a serious problem when working with:
Money
Payments
Banking
Financial calculations
Measurements
Scientific applications
Imagine calculating:
const price = 0.1;
const quantity = 3;
console.log(price * quantity);You don't want tiny floating-point errors affecting financial calculations.
How Do Developers Handle It?
One common approach is to work with integers when dealing with money.
Instead of storing:
$10.99store:
1099 centsThen calculations can use integers:
const price = 1099;
const quantity = 3;
const total = price * quantity;The result is:
3297which represents $32.97.
For more complex financial applications, developers can also use decimal or arbitrary-precision libraries designed for accurate decimal calculations.
What About Comparing Numbers?
This can also cause problems.
Avoid relying on direct equality for floating-point calculations:
0.1 + 0.2 === 0.3Instead, when appropriate, compare numbers within a small tolerance:
Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILONThe correct approach depends on the application and the required precision.
Is JavaScript Broken?
No.
JavaScript follows the IEEE 754 floating-point standard for its regular Number type.
This behavior isn't unique to JavaScript.
Similar floating-point behavior can appear in many programming languages.
The important lesson is that decimal numbers and binary floating-point numbers are not always represented in exactly the same way.
The Bigger Lesson
Computers are extremely precise, but precision and accuracy are not the same thing.
A tiny rounding error might not matter when calculating an animation or displaying a percentage.
It can matter a lot when calculating someone's bank balance.
So the next time you see:
0.1 + 0.2 = 0.30000000000000004don't think your programming language is broken.
It's simply showing you how numbers are represented inside a computer.
Understanding floating-point numbers isn't just about passing a coding interview. It's about knowing when your application's calculations can be trusted.
Comments
Join the discussion and share your thoughts.