JavaScript

Why 0.1 + 0.2 Isn't 0.3 in JavaScript (Explained Simply)

2:2413 views

Type 0.1 + 0.2 into JavaScript and you get 0.30000000000000004. It's not a bug: it's how computers store decimal numbers (IEEE 754, 64-bit floating point), and Python and PHP do exactly the same.

What you'll learn: why 0.1 can't be stored exactly in binary, why tiny rounding errors show up when you add, why it breaks code like if (total === 0.3), and 3 fixes: compare with a tolerance (Number.EPSILON), keep money in whole cents/paise, and round only for display with toFixed().

💼 Need a developer to build or fix your site hemantinsights.com/contact?type=project 📖 Guides + free developer tools: hemantinsights.com/ 🧰 300+ free online tools I built: toolyconvert.com/

Every result on screen was run for real: Node.js, Python 3 and PHP 8.3. Narration: Kokoro TTS (Apache-2.0). Music: original score. Animation: original.

HemantInsights

@hemantinsightsdev14 videos16 Shorts

↑ ↓ to move, Enter to open, Esc to close

Keyboard shortcuts

Ctrl+K
Search tools, articles and projects
/
Search
[
Previous lesson (on articles)
]
Next lesson (on articles)
T
Switch light / dark theme
GthenH
Go to the home page
GthenT
Go to the tools
GthenA
Go to the articles
?
Show this list