leonardseymore
leonardseymore

Reputation: 543

parseInt in JavaScript or AS3 returning wrong value

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

Answers (2)

Markiyan Harbych
Markiyan Harbych

Reputation: 189

I assume it's because you've reached the MAX_SAFE_INTEGER.

Please, read this article:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

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

Cobus Kruger
Cobus Kruger

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

Related Questions