Opposite of Number.toExponential in JS

前端 未结 5 637
感情败类
感情败类 2020-12-10 16:36

I need to get the value of an extremely large number in JavaScript in non-exponential form. Number.toFixed simply returns it in exponential form as a string, wh

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 17:05

    You can use toPrecision with a parameter specifying how many digits you want to display:

    x.toPrecision(31)
    

    However, among the browsers I tested, the above code only works on Firefox. According to the ECMAScript specification, the valid range for toPrecision is 1 to 21, and both IE and Chrome throw a RangeError accordingly. This is due to the fact that the floating-point representation used in JavaScript is incapable of actually representing numbers to 31 digits of precision.

提交回复
热议问题