Matt Nguyen
Matt Nguyen

Reputation: 45

javascript converting scientific notation, how to determine the number after power of 10

If I type a number in the browser console the result is:

1000000000000000000000 --> 1e+21
0.00000000003453 --> 3.453e-11

I want to determine the number after e. In this example, the number is 21 or -11

Thanks!

Upvotes: 1

Views: 84

Answers (1)

Nick Parsons
Nick Parsons

Reputation: 50684

You can convert the number to a string using .toExponential() and then split based on e and get the last element (which would be your number) like so:

const getExp = n =>
  +(n.toExponential().split('e').pop())

console.log(getExp(1000000000000000000000)) // 21
console.log(getExp(0.00000000003453)); // -11
console.log(getExp(0.000324)); // -4

Upvotes: 2

Related Questions