The toExponential()
method in JavaScript is used to format a number in exponential notation. It returns a string representation of the number in exponential form, with the specified number of digits after the decimal point.
Here’s the syntax of the toExponential()
method:
number.toExponential([fractionDigits])
number
: The number you want to convert to exponential notation.fractionDigits
(optional): The number of digits to appear after the decimal point (default is as many as necessary).
The toExponential()
method is available on all number values in JavaScript, and it returns a string representation of the number in exponential notation.
JavaScript toExponential() Method example
Simple example code that demonstrates the usage of the toExponential()
method in JavaScript:
const number1 = 1234.5678;
const number2 = 0.012345;
console.log(number1.toExponential()); // Output: 1.2345678e+3
console.log(number1.toExponential(2)); // Output: 1.23e+3
console.log(number1.toExponential(5)); // Output: 1.23457e+3
console.log(number2.toExponential()); // Output: 1.2345e-2
console.log(number2.toExponential(2)); // Output: 1.23e-2
console.log(number2.toExponential(5)); // Output: 1.23450e-2
Output:
In the above example, we have two numbers, number1
and number2
. The toExponential()
method is called on these numbers with different fractionDigits
values.
Do comment if you have any doubts or suggestions on this Js method.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version