JavaScript Reference ยท Number and BigInt
Number.prototype.toExponential() method
Reference for JavaScript toExponential(): format a number in exponential (scientific) notation, choose the digits, and handle NaN, Infinity and RangeError.
toExponential() returns a string that writes a number in exponential (scientific) notation: one digit before the decimal point, some digits after it, and a power of ten, as in "1.5e+3". It is handy for very large or very small values, and for showing a fixed number of significant digits.
The method belongs to numbers, so it is called on a number value or a Number object. It never changes the number; it only returns a new string.
(1500).toExponential(); // "1.5e+3" (0.00042).toExponential(1); // "4.2e-4"
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
numberValue.toExponential() numberValue.toExponential(fractionDigits)
| Name | Description |
|---|---|
fractionDigits |
Optional. How many digits to show after the decimal point, from 0 to 100. If omitted, the result uses as many digits as are needed to represent the number uniquely. |
Return value: a string such as "1.23e+4". The exponent always has a sign (+ or -). For NaN, Infinity and -Infinity the method returns "NaN", "Infinity" and "-Infinity".
Exceptions: a RangeError if fractionDigits is outside 0 to 100. A TypeError if the method is called on something that is not a number.
Usage notes
- The result is a string. Convert it back with
Number("1.5e+3")if you need arithmetic; theeform is valid numeric text. - Leave the argument out to keep full precision. You then get the shortest digits that identify the number, which matches how
String(number)works for large and small values. - With an argument, the value is rounded to that many fractional digits. The result can be slightly different from what decimal arithmetic on paper suggests, because the stored binary value may not be exactly the decimal you typed (see floating-point numbers).
- Zero keeps its zeros.
(0).toExponential(2)is"0.00e+0", with an exponent of0. - Number literals need care.
5.toExponential()is a syntax error because5.is read as a decimal number. Use(5).toExponential()or a variable. - For fixed decimals without an exponent use
toFixed(). For a total count of significant digits,toPrecision()picks fixed or exponential form for you. - For display to users, prefer
toLocaleString()withnotation: "scientific"when the format should follow the reader's locale.
Browser support
toExponential() is available in every browser and JavaScript runtime. The ability to pass a fractionDigits value up to 100 is part of modern engines; very old engines accepted a smaller range.
Related
toFixed(),toPrecision(),toString(),toLocaleString()- Lessons: Numbers, Math