Every JavaScript developer has a villain origin story.
For some, it is undefined is not a function.
For others, it is CSS.
But for many of us, it starts here:
console.log(0.1 + 0.2);
Expected result:
0.3
JavaScript:
0.30000000000000004
Excuse me?
I asked for math, not a philosophical debate.
Why does this happen?
JavaScript stores numbers as 64-bit floating-point values using the IEEE 754 standard.
That means decimal numbers like 0.1 and 0.2 cannot be represented perfectly in binary.
So JavaScript does its best.
Unfortunately, “its best” sometimes looks like this:
console.log(0.3 / 0.1);
// 2.9999999999999996
Technically correct.
Emotionally damaging.
The real lesson
This is not really a JavaScript bug.
It is how floating-point numbers work in many programming languages.
But it becomes dangerous when you are working with:
- money
- pricing
- invoices
- billing
- accounting
- discounts
- tax calculations
Basically, anything where your user does not want to hear:
“Your total is approximately $19.999999999997.”
What should you do?
For money, avoid direct floating-point calculations.
Use cents instead:
const priceInCents = 1999;
const taxInCents = 200;
const total = priceInCents + taxInCents;
console.log(total / 100);
// 21.99
Or use a proper decimal library when precision really matters.
Final thought
AI coding agents are amazing.
They can write functions, tests, APIs, components, workflows, and sometimes even explain your own code better than you can.
But if your AI agent writes payment logic using floating-point math without tests…
Maybe do not approve that pull request too quickly.
Trust the agent.
But verify the decimals.
Top comments (0)