๐—ง๐—ต๐—ฒ ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐—•๐˜‚๐—ด ๐—ช๐—ถ๐˜๐—ต ๐—ก๐—ผ ๐—™๐—ถ๐˜…

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:

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