Using Number toExponential() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the Number method referred to as toExponential() with syntax and examples.

Description

In JavaScript, toExponential() is a Number method that is used to convert a number to exponential notation and return its value as a string. Because toExponential() is a approach of the Number object, it should be invoked through a specific instance of the Number class.

Syntax

In JavaScript, the syntax for the toExponential() method is:

number.toExponential([fractionalDigits]);

Parameters or Arguments

fractionalDigits Optional. It is the quantity of fractional digits to show in the result. If this parameter is omitted, up to 16 fractional digits might also show up in the result, depending on your browser.

Returns

The toExponential() technique converts a wide variety to exponential notation and then returns its price as a string.

Note

The toExponential() method does not change the cost of the original number.

Example

Let’s take a appear at an example of how to use the toExponential() approach in JavaScript.

For example:

var totn_number = 123.456789;

console.log(totn_number.toExponential());
console.log(totn_number.toExponential(1));
console.log(totn_number.toExponential(2));

In this example, we have declared a variable known as totn_number that is assigned the price of 123.456789. We have then invoked the toExponential() method of the totn_number to convert the number to exponential notation represented as a string.

We have written the output of the toExponential() technique to the internet browser console log, for demonstration purposes, to exhibit what the toExponential() approach returns.

The following will be output to the web browser console log:

1.23456789e+2
1.2e+2
1.23e+2

In this example, the first output to the console log returned the string cost “1.23456789e+2” which is the exponential notation for the quantity 123.456789.

The 2nd output to the console log lower back the string price “1.2e+2” which is the exponential notation for the range 123.456789 with 1 fractional digit.

The third output to the console log again the string value “1.23e+2” which is the exponential notation for the range 123.456789 with two fractional digits.