0
money = 0.56;
money -= 0.50; // here the result is 0.06000000000000005 when actually i need it to be just 0.06
money -= 0.06  // and after this subtraction the result becomes 5.0306980803327406e-17
console.log(money); // 5.0306980803327406e-17

This totally messes up my logic when for example I compare "money" with the number 0.

Alexander
  • 35
  • 5
  • Read this https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Buszmen Feb 16 '20 at 19:21
  • 1
    Does this answer your question? [Simple subtraction bug in Javascript](https://stackoverflow.com/questions/5153061/simple-subtraction-bug-in-javascript) – J0B Feb 16 '20 at 19:22
  • I see ! That's kind of stupid though . – Alexander Feb 16 '20 at 19:33
  • @Alexander if you're dealing with money, the industry standard is to use fixed-point math. Essentially treat every quantity as a number of cents (or a number of thousandths), instead of a number of dollars. So $12.50 - $4.99 would be 12500 - 4990. Using integers prevents floating point arithmetic errors. – PitaJ Feb 16 '20 at 20:04

0 Answers0