简体   繁体   中英

Convert large number in javascript

After pasting the number t=3.7333333258105216E16 in jsconsole.com or in Web Inspector, I get 37333333258105220 .

parseFloat(3.7333333258105216E16) gives the same result.

What is the reason ?

JavaScript represents numbers as floats. This storage format consists of 64 bits. 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.

Because of the above, numbers can be acurate to the 1/2^52 , or 1 / 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.

I tested this out by trying to add one to 4,503,599,627,370,495 . It gets to 4,503,599,627,370,496 , but does not get past it. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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