Javascript Reference - JavaScript toExponential() Method








The toExponential() method converts a number into an exponential notation.

toExponential() method format the number in exponential notation. toExponential() accepts the number of decimal places to output as the argument.


var num = 10; 
console.log(num.toExponential(1)); //"1.0e+1" 

The code above generates the following result.

Browser Support

toExponential Yes Yes Yes Yes Yes

Syntax

number.toExponential(x)




Parameter Values

Parameter Description
x Optional. An integer between 0 and 20 representing the number of digits after the decimal point.

Return Value

A String type value representing the number as an exponential notation.

Example

The following code shows how to convert a number into an exponential notation.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from  w w  w .  j av  a 2s  . co  m-->
<p id="demo"></p>

<script>
function myFunction() {
    var num = 5.56789;
    var n = num.toExponential();
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to convert a number into an exponential notation.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!-- ww  w  . ja v  a  2 s .co m-->
<p id="demo"></p>

<script>
function myFunction() {
    var num = 5.56789;
    var n = num.toExponential(3);
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

The code above is rendered as follows: