我有以下代码:
var str = "0x4000000000000000"; //4611686018427387904 decimal var val = parseInt(str); alert(val);我得到这个值:4611686018427388000",即0x4000000000000060
我想知道是 JavaScript 对 64 位整数处理不当还是我做错了什么?
I was wondering if JavaScript is mishandling 64-bit integers or am I doing something wrong?
推荐答案JavaScript 使用 IEEE- 表示数字754 双精度(64 位)格式.据我了解,这为您提供了 53 位精度,或 15 到 16 位十进制数字.您的数字的位数超出了 JavaScript 的处理能力,因此您最终会得到一个近似值.
JavaScript represents numbers using IEEE-754 double-precision (64 bit) format. As I understand it this gives you 53 bits precision, or fifteen to sixteen decimal digits. Your number has more digits than JavaScript can cope with, so you end up with an approximation.
这并不是真正的处理不当",但如果您需要大数的全精度,显然这不是很有帮助.有一些 JS 库可以处理更大的数字,例如,BigNumber 和 Int64.
This isn't really "mishandling" as such, but obviously it isn't very helpful if you need full precision on large numbers. There are a few JS libraries around that can handle larger numbers, e.g., BigNumber and Int64.