๐ง๐ต๐ฒ ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ด ๐ช๐ถ๐๐ต ๐ก๐ผ ๐๐ถ๐
Many JS developers face a weird math problem.
You type 0.1 + 0.2. You expect 0.3. JS gives you 0.30000000000000004.
JS uses the IEEE 754 standard. Binary does not store these decimals perfectly. JS does its best. The result is often slightly off.
This is dangerous for money.
- Pricing
- Invoices
- Tax calculations
Users do not want a total of 19.999999999997 dollars.
Avoid direct decimal math for money. Use cents. 1999 cents is better than 19.99. Use a decimal library for high precision.
AI agents write code. They write functions and APIs. AI agents often use floating-point math for payments. Review those pull requests. Trust the agent. Verify the decimals.
Source: https://dev.to/marrouchi/the-javascript-bug-that-will-never-get-fixed-3618