0

Text

402.39 - 226.96 = 175.43

I took this screenshot from my MSE console. This is not complex calculations. I've been scratching my head into why my equations were not giving me the right numbers until I decided to try the equation in Edge's console to find out that the problem seems to be the Browser and not me.

Am I missing something, did I forget anything?

Since I could not figure out the problem in my code, I chose to hard code values in my variable and reduce my code to the plain minimum:

var ltotal = 402.39 - 226.96;
document.getElementById("txtotal").value = ltotal.toString();

Initially, I thought I needed to round. But it's absolutely unecessary to round anything here. Clearly something is not correct or I missed something.

Is there a javascript rule I may have forgoten?

David Richard
  • 71
  • 1
  • 8
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken –  Mar 08 '21 at 03:02
  • Not a javascript rule, just a computing rule (C++, Java, Go, Javascript, etc..) - you forgot about how floating point numbers work – slebetman Mar 08 '21 at 03:08
  • 1
    This occurs because of limitations in how floating point numbers are represented in binary. See the link provided. There's nothing wrong with your code, this is expected, and will occur in every browser and other programming languages. If you convert the input numbers into binary by hand, you'll see that they cannot be exactly represented. –  Mar 08 '21 at 03:13
  • Also see https://modernweb.com/what-every-javascript-developer-should-know-about-floating-points/ –  Mar 08 '21 at 03:13

0 Answers0