Course contentsShow
← JavaScript
Lesson 116 of 2,7234. Working with NumbersFree lesson

Why 0.1 + 0.2 !== 0.3

Understand the classic floating-point precision issue and why decimal arithmetic can be inexact.

Why 0.1 + 0.2 !== 0.3

You'll learn why JavaScript (and most programming languages) can't always add decimal numbers exactly.

The Classic Floating-Point Surprise

Try this in your console:

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

Wait, what? Simple arithmetic fails? This isn't a bug—it's how computers store decimal numbers.

Why This Happens

Remember from "Floating-Point Representation Basics" that JavaScript stores numbers in binary (base-2), not decimal (base-10). The problem is that some decimal fractions can't be represented exactly in binary.

An analogy: Try writing 1/3 in decimal. You get 0.333333... repeating forever. Similarly, 0.1 in binary is 0.0001100110011... repeating forever. The computer must round it to fit in 64 bits, creating tiny errors.

When you add 0.1 and 0.2, you're actually adding two already-rounded approximations. Those tiny errors combine, giving you 0.30000000000000004 instead of exactly 0.3.

What You Can Do

For comparisons, check if numbers are "close enough" instead of exactly equal:

let sum = 0.1 + 0.2;
console.log(Math.abs(sum - 0.3) < Number.EPSILON);  // true

This uses Number.EPSILON (the smallest difference between two representable numbers) to allow tiny rounding errors.

Key Takeaway: Decimal arithmetic in JavaScript isn't always exact because numbers are stored in binary floating-point format—expect tiny rounding errors when working with fractions.