Reputation: 543
Can someone please explain why all these parseInt operations evaluate to the same number 10153593963283296?
parseInt('10153593963283294') 10153593963283294
parseInt('10153593963283295') 10153593963283296
parseInt('10153593963283296') 10153593963283296
parseInt('10153593963283297') 10153593963283296
parseInt('10153593963283298') 10153593963283298
Tested in browsers and node command line.
Thanks!
Upvotes: 4
Views: 99
Reputation: 189
I assume it's because you've reached the MAX_SAFE_INTEGER.
Please, read this article:
There's a note:
"The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1.
Safe in this context refers to the ability to represent integers exactly and to correctly compare them."
Upvotes: 1
Reputation: 8605
Your number is larger than Number.MAX_SAFE_INTEGER. JavaScript stores even Integers as floating-point numbers and therefore you lose precision once your number becomes too large.
Upvotes: 4