Consider the following JavaScript code:
alert(9999999999999999);
When I am executing this file, I get the following output:
Why I am getting 100000000000
in alert()
? Can anyone give suggestions please?
Consider the following JavaScript code:
alert(9999999999999999);
When I am executing this file, I get the following output:
Why I am getting 100000000000
in alert()
? Can anyone give suggestions please?
This is an issue with the way JavaScript deals with numbers. The largest possible integer value in JavaScript is 9007199254740992.
To compare:
Your number: 9007199254740992
Largest num: 9999999999999999
So if you test 9007199254740992 it will work fine, but one number more (9007199254740993) and JavaScript will return 9007199254740992 still.
To give further detail, JavaScript numbers are 64-bit floating point values, the largest exact integral value is 253.
It's represented as double, and that number cannot be stored exactly as a double because it has too many significant digits. The result you got back is the closest double that can be represented.