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.
toExponential |
Yes | Yes | Yes | Yes | Yes |
number.toExponential(x)
Parameter | Description |
---|---|
x | Optional. An integer between 0 and 20 representing the number of digits after the decimal point. |
A String type value representing the number as an exponential notation.
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:
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: