JS: Number.prototype.toExponential

By Xah Lee. Date: . Last updated: .

number.toExponential ( fractionDigits )

Returns a string representation of number in exponential notation, with fractionDigits fraction digits.

Example of return string "3.12e+2". (the “e+2” means multiply by 10^2)

If fractionDigits is undefined, include as many significand digits as necessary to uniquely specify the number (just like in ToString except that in this case the number is always output in exponential notation).

let num = 3.12345;

console.log (
 num.toExponential ( 1 ) === "3.1e+0" ); // true

console.log (
 num.toExponential ( 2 ) === "3.12e+0" ); // true

console.log (
 num.toExponential ( 3 ) === "3.123e+0" ); // true
let num = 12.345;

console.log (
 num.toExponential ( 1 ) === "1.2e+1" ); // true

console.log (
 num.toExponential ( 2 ) === "1.23e+1" ); // true

console.log (
 num.toExponential ( 3 ) === "1.235e+1" ); // true
let num = 0.123;

console.log (
 num.toExponential ( 1 ) === "1.2e-1" ); // true

console.log (
 num.toExponential ( 2 ) === "1.23e-1" ); // true

console.log (
 num.toExponential ( 3 ) === "1.230e-1" ); // true

Reference

ECMAScript® 2016 Language Specification#sec-number.prototype.toexponential

Number Topic

  1. JS: Format Number
  2. JS: String to Number
  3. JS: Number Object
  4. JS: Number Constructor
  5. JS: Number.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon