Did you ever try to convert a big number to a string in javascript?
Please try this:
var n = 10152557636804775;
console.log(n); // outputs 10152557636804776
Can you help me understand why?
Did you ever try to convert a big number to a string in javascript?
Please try this:
var n = 10152557636804775;
console.log(n); // outputs 10152557636804776
Can you help me understand why?
10152557636804775
is higher than the maximum integer number that can be safely represented in JavaScript (it's Number.MAX_SAFE_INTEGER
). See also this post for more details.
From MDN (emphasis is mine):
The MAX_SAFE_INTEGER constant has a value of 9007199254740991. 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 -(2^53 - 1) and 2^53 - 1.
To check if a given variable can be safely represented as an integer (without representation errors) you can use IsSafeInteger()
:
var n = 10152557636804775;
console.assert(Number.isSafeInteger(n) == false);