๐ง๐ต๐ฒ ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐๐ด ๐ช๐ถ๐๐ต ๐ก๐ผ ๐๐ถ๐
Every JavaScript developer has a story. For some, it is undefined is not a function. For others, it is CSS. For many, it starts here: console.log(0.1 + 0.2);
You expect 0.3. JavaScript gives you 0.30000000000000004.
JavaScript uses 64-bit floating-point values. Binary does not represent decimals perfectly. JavaScript does its best. The result is often wrong. console.log(0.3 / 0.1); Result: 2.9999999999999996.
This happens in many languages. It is dangerous for:
- Money
- Pricing
- Invoices
- Billing
- Accounting
- Tax calculations
Users hate seeing $19.999999999997 on a bill.
Avoid floating-point for money. Use cents instead. const priceInCents = 1999; const taxInCents = 200; const total = priceInCents + taxInCents; console.log(total / 100); Result: 21.99.
Use a decimal library for high precision.
AI agents write code. They write functions, tests, and APIs. Some AI agents write payment logic using floating-point math. Check the pull request. Trust the agent. Verify the decimals.
Source: https://dev.to/marrouchi/the-javascript-bug-that-will-never-get-fixed-3618