Why does parsing a large number of string form give a larger number?
See snippet:
var num = "9223372036854775808";
document.write(num);
document.write("<br>");
document.write(parseInt(num, 10));
When running the code snippet, the first entry gives:
9223372036854775808
and the third result of writing:
9223372036854776000
But I am just parsing a number in string form into a number. Why is he giving even more?
I thought that maybe something needs to be done with the storage capacity limits, but then why would he get a larger number if he couldn't store a smaller one?
I read this question: Why parsing a very large number to integers returns 1 and parseInt
doc , but they didn't help much.
So why does parsing a large number of a string form return a larger number?
source to share
The first result is accurate because it is treated as a string.
In the second, it crosses the int value which is +/- 9007199254740992 i.e. the maximum value that parseint can parse is 9007199254740992, and since your value 9223372036854775808 is greater than the maximum value. it gives some value to the trash.
As blunderboy correctly comments, it seems that this could be the cause
Numbers in Javascript are 64-bit "double" precision followed by IEE754 floating point. So the largest positive integer that can be accurately represented is 2 ^ 53, and the rest of the remaining bits are reserved for the exponent.
If you check the ECMA specs , this is explained.
source to share