简体   繁体   English

在javascript中转换大量

[英]Convert large number in javascript

After pasting the number t=3.7333333258105216E16 in jsconsole.com or in Web Inspector, I get 37333333258105220 . 在jsconsole.com或Web Inspector中粘贴数字t=3.7333333258105216E16之后,我得到了37333333258105220

parseFloat(3.7333333258105216E16) gives the same result. parseFloat(3.7333333258105216E16)给出相同的结果。

What is the reason ? 是什么原因 ?

JavaScript represents numbers as floats. JavaScript将数字表示为浮点数。 This storage format consists of 64 bits. 此存储格式由64位组成。 One bit is for the sign, 11 bits are for the power of 10 to multiply the number by, and 52 bits are for the number. 一位是符号,11位是10的幂乘以该数字,52位是该数字。

Because of the above, numbers can be acurate to the 1/2^52 , or 1 / 4,503,599,627,370,496 . 由于上述原因,数字可以精确到1/2^521 / 4,503,599,627,370,496 Thus, numbers are accurate to within this fraction. 因此,数字精确到该分数之内。 Check out this wikipedia page for more information on floating point numbers. 请查看此Wikipedia页面 ,以获取有关浮点数的更多信息。

I tested this out by trying to add one to 4,503,599,627,370,495 . 我尝试通过将4,503,599,627,370,495加1来进行4,503,599,627,370,495 It gets to 4,503,599,627,370,496 , but does not get past it. 它达到4,503,599,627,370,496 ,但没有超过它。 Here's the fiddle for testing. 这是测试的小提琴。

You are encountering floating-point roundoff. 您遇到浮点舍入。 JavaScript numbers are implemented as double precision, 64-bit floats according to the IEEE 754 standard. 根据IEEE 754标准,JavaScript数字被实现为双精度64位浮点数。

You can't always accurately represent a floating point decimal number in binary. 您不能总是准确地用二进制表示浮点十进制数。 It is losing precision at the end of the number so it can fit in 64 bits. 它在数字末尾失去了精度,因此可以容纳64位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM