Skip to content

Number.toExponential()

Why toExponential ?

The JavaScript prototype.toExponential () method is used to convert the entered value exponentially. This method returns a string representing the Number object.

Syntax:

number.toExponential( [fractionDigits] )

  • fractionDigits ( optional ): An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.

Exceptions:

  1. Range Error: This exception is thrown when the value parameter passed is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. If you want to pass larger or smaller values than specified by this range then you have to accordingly implement the toExponential() function.
  2. Type Error: This exception is thrown when the toFixed() method is invoked on an object that is not of type number.

JavaScript toExponential Examples

Example 1

        let number = 77.1234;
        let val = number.toExponential();


        val = number.toExponential(2);
        console.log("num .toExponential(2) is : " + val);

        val = number.toExponential(9);
        console.log("num .toExponential(9) is : " + val);


        val = 77.1234.toExponential();
        console.log("45.4659 .toExponential()is : " + val);


        val = 77.1234.toExponential();
        console.log("77 .toExponential() is : " + val);

output:

num .toExponential(2) is : 7.71e+1
num .toExponential(9) is : 7.712340000e+1
45.4659 .toExponential()is : 7.71234e+1
77 .toExponential() is : 7.71234e+1


Example 2

        let toExoo = (x,y) => {
            return Number.parseFloat(x).toExponential(y);
        }

        console.log(toExoo(123, 2));
        console.log(toExoo(000, 5));
        console.log(toExoo(11, 10));
        console.log(toExoo(1998, 1));
        console.log(toExoo(11, 1));
        console.log(toExoo(55, 25));
        console.log(toExoo(999, 3));

output:

1.23e+2
50 0.00000e+0
1.1000000000e+1
2.0e+3
1.1e+1
5.5000000000000000000000000e+1
9.990e+2


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: