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:
- 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.
- 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
| Chrome | yes |
| Edge | yes |
| Firefox | 1 |
| Internet Explorer | yes |
| Opera | yes |
| Safari | yes |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |