๐ง๐ต๐ฒ ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ด ๐ช๐ถ๐ถ๐๐ต ๐ก๐ผ ๐๐ถ๐
Every JavaScript developer has a bad story.
Try adding 0.1 and 0.2 in JavaScript. You expect 0.3. You get 0.30000000000000004.
JavaScript uses 64-bit floating point values. Binary cannot represent decimals like 0.1 and 0.2 perfectly. The language does its best.
This is not a bug. Many programming languages work this way.
This is risky for:
- Money
- Pricing
- Invoices
- Taxes
Your users do not want to see $19.999999999997.
Avoid decimals for money. Use cents instead.
- Price: 1999 cents
- Tax: 200 cents
- Total: 2199 cents
- Final result: 21.99
Use a decimal library for high precision.
AI agents write code fast. They build functions and APIs. But AI agents use floating point math for payments. Do not approve those pull requests too fast.
Trust the agent. Verify the decimals.
Source: https://dev.to/marrouchi/the-javascript-bug-that-will-never-get-fixed-3618 Optional learning community: https://t.me/GyaanSetuAi