4

In Javascript following number

9999999999999999.99

Gets converted in to

10000000000000000

How can I prevent this behaviour?

1
  • 2
    this is a floating point precision problem ... beyond 9007199254740991 even integers start behaving oddly ... for instance 9007199254740992 == 9007199254740993 is true Feb 22, 2016 at 15:45

1 Answer 1

1

It's my understanding that the best way to deal with this is to do all your calculations in whole numbers, and divide by 100 (or plus as many zeros as needed) afterwards.

If you absolutely need to use decimals, this post contains a lot of useful information that should be of use to you.

1
  • I'm not sure this would help in this case. The closest IEEE 754 64-bit binary float to 999999999999999999 is 1000000000000000000. It could be represented exactly using e.g. Java long, which has 64 bits, but I don't think that is available in JavaScript. Feb 22, 2016 at 16:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.