JavaScript parseInt转换有问题

前端之家收集整理的这篇文章主要介绍了JavaScript parseInt转换有问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > 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 of 9007199254740991 (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.

原文链接:https://www.f2er.com/js/158200.html

猜你在找的JavaScript相关文章