Search code examples
javascriptparseint

In JavaScript, I found parseInt("fffffffffffff2",16) has a wrong result sometime


In JavaScript console:

parseInt("fffffffffffff2",16)
parseInt("fffffffffffff3",16)
parseInt("fffffffffffff4",16)

All have the same result which is 72057594037927920.

I would like to know why.


Solution

  • The highest certain accuracy number in javascript is +/-9007199254740992 (2^53). Thats because javascript stores all numbers as 64 bit floating points. So inherently they will only be accurate up to a certain point because of rounding errors. Double floating points have 53 bits of precision so the max certain accurate value stated above applies. You could still get an accurate representation of the value but its not guaranteed after this point.