toExponential() Method – How to Convert Numbers to Exponential Notations
toExponential() converts a number to its exponential equivalent (Scientific Notation).
In other words, whenever you use toExponential()
on a number, the method does the following:
- It places a decimal point after the first digit of the specified number.
- It uses an exponential notation to express how many places it moved the decimal point.
For instance, consider the following number:
703.59
7.0359e+2
is the exponential equivalent of the number above because we moved the decimal point twice from its initial position to its final place.
An e+2 exponential notation means the number’s decimal point moved two places to the left.
Syntax of the toExponential()
Method
toExponential()
accepts only one optional argument. Here is the syntax:
number.toExponential(totalFractionDigits);
The totalFractionDigits
argument specifies the number of digits browsers should show after the decimal point. If omitted, browsers will use as many digits as necessary to represent the number.
Examples of the toExponential()
Method
Below are examples of the toExponential()
method.
Convert 703.59
to an exponential notation
const number = 703.59;
number.toExponential();
// The invocation above will return: 7.0359e+2
Convert 7.0359
to an exponential notation
const number = 7.0359;
number.toExponential();
// The invocation above will return: 7.0359e+0
Convert 0.70359
to an exponential notation
const number = 0.70359;
number.toExponential();
// The invocation above will return: 7.0359e-1
Convert 0.000070359
to an exponential notation
const number = 0.000070359;
number.toExponential();
// The invocation above will return: 7.0359e-5
Convert 703.59
to an exponential notation with no decimal digits
const number = 703.59;
number.toExponential(0);
// The invocation above will return: 7e+2
Convert 703.59
to an exponential notation with only one decimal digit
const number = 703.59;
number.toExponential(1);
// The invocation above will return: 7.0e+2
Convert 703.59
to an exponential notation with three decimal digits
const number = 703.59;
number.toExponential(3);
// The invocation above will return: 7.036e+2
Convert 703.59
to an exponential notation with seven decimal digits
const number = 703.59;
number.toExponential(7);
// The invocation above will return: 7.0359000e+2