How to use scientific notation in js?

前端 未结 3 357
萌比男神i
萌比男神i 2021-01-18 17:45
var a = 5.0;
var b = a * 10e-12;
b *= 10e+12
print(b)

Why b equals 500 instead of 5?

As far as I know 10^(-12) equals to 1/(10^12), how can

3条回答
  •  遥遥无期
    2021-01-18 18:28

    Because math. 10e1 is 100 and 10e-1 is 1.

    • 10e1 * 10e-1 is 100.
    • 10e2 * 10e-2 is 100.
    • 10e3 * 10e-3 is 100.

    You can very easily extend this to figure out that 10eN * 10e-N is always going to be 100.

    If you want actual scientific notation, as in 1 * 10 ^ 2, you want 1e12 and 1e-12.

提交回复
热议问题