4

In Javascript following number

9999999999999999.99

Gets converted in to

10000000000000000

How can I prevent this behaviour?

Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
JS-JMS-WEB
  • 2,555
  • 3
  • 17
  • 26
  • 2
    this is a floating point precision problem ... beyond 9007199254740991 even integers start behaving oddly ... for instance `9007199254740992 == 9007199254740993` is **true** – Jaromanda X Feb 22 '16 at 15:45

1 Answers1

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.

Community
  • 1
  • 1
millerbr
  • 2,951
  • 1
  • 14
  • 25
  • 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. – Patricia Shanahan Feb 22 '16 at 16:51