toExponential()
is a Number
method that formats a string into an exponential notation. The toExponential()
method is declared as follows:
num.toExponential(len)
num
: The number to be converted to an exponential notation.len
: The number of digits after the decimal point.Note:
- If
len
is not passed as a parameter, it is set to the number of digits necessary to represent the number.len
must be in the range 1 to 100. Otherwise, RangeError is thrown.
The toExponential()
method returns a string that represents the number num
in exponential notation.
The toExponential()
method is supported by the following browsers:
Consider the code snippet below, which demonstrates the use of the toExponential()
method:
var num = 15.199console.log("15.199 to precision of no length is: ",num.toExponential());console.log("15.199 to precision of length 3 is: ",num.toExponential(0));console.log("15.199 to precision of length 7 is: ",num.toExponential(7));
A number num
is initialized with num = 15.199
in line 1.
toExponential()
method is not passed the input parameter, so len
is set to len = 4
, as num
has 5 digits and the first digit is placed before the decimal point. This is why we need 4 decimal places.len = 0
is passed to the toExponential()
method, so there is no decimal place, and num
is rounded to a whole number.len = 7
is passed to the toExponential()
method, so 3 zeros are added to make 7 decimal places.Free Resources