Balraj McCoy Balraj McCoy - 2 months ago 7
Javascript Question

How do I format a number in scientific notation to show less digits in Javascript?

For example, I have a number which is 1.3685999999999998e+35 but I just want to display 1.368e+35. I tried toFixed and toPrecision but nothing works.

Answer

You need to use Number.prototype.toExponential:

var x = 11827361827361872361263812631827361823618723;
console.log(x.toExponential(2));
console.log(x.toExponential(3));
console.log(x.toExponential(10));