本文介绍了JavaScript不能处理64位整数,可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果JavaScript是误操作的64位整数还是我做错了什么?

I was wondering if JavaScript is mishandling 64-bit integers or am I doing something wrong?

我有以下的code:

var str = "0x4000000000000000";   //4611686018427387904 decimal
var val = parseInt(str);
alert(val);

我得到这个值:4611686018427388000,这是0x4000000000000060

I get this value: "4611686018427388000", which is 0x4000000000000060

推荐答案

JavaScript的重新presents号码使用 IEEE-754 的双precision(64位)的格式。据我所知,这给你53位precision,或者十五到十六位小数。您的号码比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.

这是不是真正的误操作正因为如此,但显然不是很有益的,如果你需要的大量全precision。有一些JS库周围,可以处理更大的数字,比如, BigNumber

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.

这篇关于JavaScript不能处理64位整数,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 19:29