user983302
user983302

Reputation: 1437

How to use scientific notation in js?

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 i rewrite the code?

Upvotes: 16

Views: 25470

Answers (2)

g.kertesz
g.kertesz

Reputation: 454

"As far as I know 10^(-12) equals to 1/(10^12)" -- that is correct, but 10e-12 actually means 10*10^(-12)

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179046

10-12 × 1012 = 1

But what you wrote wasn't 10-12, nor did you write 1012.

What you wrote was 10 × 1012 and 10 × 10-12:

10 × 1012 × 10 × 10-12 = 100

100 × 5 = 500

Proper scientific notation is 1e-12 and 1e12. The e stands for "ten to the power of", so you don't need to multiply that value by ten again.

Upvotes: 15

Related Questions