0

I am using the Math.exp function with the .toFixed() method, but results that are expressed in scientific notation seem to ignore the method. Correct me if I am wrong, but I understand that .toPrecision() handles scientific notation better than does .toFixed().

Is there a way to invoke the .toPrecision() method only if a result is expressed in scientific notation?

newbie2015
  • 251
  • 3
  • 17

1 Answers1

0

toPrecision isn't better than toFixed, they're just different. See https://stackoverflow.com/a/3337892/1378139.

If you'd like to invoke toPrecision only when Math.exp(n) would result in scientific notation, I guess you could exclude cases specifically, like:

var result = n !== 0 ? Math(n).toPrecision() : Math(n).toFloat();
Community
  • 1
  • 1
jonaz
  • 2,096
  • 1
  • 17
  • 30