参见英文答案 >
How is the parseInt in JavaScript defined to handle large “numbers” – is there an ECMA leak? I got a wow here3个
跑步时
跑步时
console.log(parseInt("9658921879781125"))
它给出的值为9658921879781124,小于原始值.
为什么会这样?
解决方法
那是因为
9658921879781125 > Number.MAX_SAFE_INTEGER // true
因此,尝试使用大于Number.MAX_SAFE_INTEGER
的数字是不安全的
The
MAX_SAFE_INTEGER
constant has a value of9007199254740991
(9,007,199,254,740,991 or ~9 quadrillion). 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.