Why is 9999999999999999 converted to 10000000000000000 in JavaScript?

后端 未结 5 2099
遥遥无期
遥遥无期 2020-11-30 03:45

Can any one explain to me why 9999999999999999 is converted to 10000000000000000?

alert(9999999999999999); //10000000000000000

http://jsfid

5条回答
  •  天涯浪人
    2020-11-30 04:40

    Question: Sometimes JavaScript computations seem to yield "inaccurate" results, e.g. 0.362*100 yields 36.199999999999996. How can I avoid this?

    Answer: Internally JavaScript stores all numbers in double-precision floating-point format, with a 52-bit mantissa and an 11-bit exponent (the IEEE 754 Standard for storing numeric values). This internal representation of numbers may cause unexpected results like the above. Most integers greater than 253 = 9007199254740992 cannot be represented exactly in this format. Likewise, many decimals/fractions, such as 0.362, cannot be represented exactly, leading to the perceived "inaccuracy" in the above example. To avoid these "inaccurate" results, you might want to round the results to the precision of the data you used.

    http://www.javascripter.net/faq/accuracy.htm

提交回复
热议问题