What is the toExponential() method in TypeScript?

Overview

The toExponential() method is used to convert a number into an exponential notation.

Syntax

Here is the syntax of the toExponential() method:

number.toExponential(x)

Parameter value

This method takes a number as a parameter to represent the number of digits after a decimal point. The number must be in the range of 0 to 20.

Return value

​​This method returns a string value that represents the exponential notation of a given number.

Let's look at an example of this.

Example

let num = 6.57769;
//no.of digits after decimal is 1
console.log(num.toExponential(1))
//no.of digits after decimal is 3
console.log(num.toExponential(3))
//no.of digits after decimal is 8
console.log(num.toExponential(8))

Explanation

  • Line 1: We declare and initialize the number num.
  • Line 4: We convert the given number num to an exponential form, which has 1 digit after the decimal point.
  • Line 7: We convert the given number num to an exponential form, which has 3 digits after the decimal point.
  • Line 10: We convert the given number num to an exponential form, which has 8 digits after the decimal point.

          Free Resources