toExponential() - Round off numbers in exponential notation


Fri Jan 10 2020 19:00:00 GMT+0000 (UTC)

Saved by @goblindoom95 #javascript #jsfunctions #numbers

var x = 1.234;
var y = x.toExponential(2); 
// returns y = 1.23e+0
var y = x.toExponential(5);  
// returns y = 1.23450e+0

line 1: assign a number value to the variable x line 3 & 6: rounds off the value of x in exponential notation. The number in the brackets - the parameter - defines the number of characters beyond the decimal point. Value of y is a string. NOTE: it is not necessary to store the value of toExponential() in a new variable (like in the example). Also, the parameter is optional. If you don't specify it, JavaScript will not round the number.