问题描述
我有以下代码:
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.
这篇关于JavaScript 是否支持 64 位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!