The toExponential() method in javaScript is used to convert a number into exponential form. It returns a string representing a Number object in exponential notation. Let's take a closer look at how to use toExponential().
Let’s first take a look at the basic syntax of toExponential()
number .toExponential(value)
The toExponential() function accepts a single parameter value. It is an optional parameter that represents a value that specifies the number of digits after the decimal point.
Let’s look at a specific example
Pass a number as a parameter in the toExponential() method. If a number is passed as a parameter to the toExponential() method, it represents the number of digits after the decimal point.
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var num = 2.13456; document.write(num.toExponential(2)); </script> </body> </html>
The output result is: 2.13e 0
No parameters are passed in the toExponential() method
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var num = 2.13456; document.write(num.toExponential()); </script> </body> </html>
The output result is: 2.13456e 0
Pass zero as a parameter in the toExponential() method
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var num = 2.13456; document.write(num.toExponential(0)); </script> </body> </html>
The output result is: 2e 2
The last thing to note is:
Range error: This exception is thrown when the value parameter passed is too small or too large. Values between 0 and 20, inclusive, do not cause a RangeError. If you want to pass a value larger or smaller than specified by this range, you must implement the toExponential() function accordingly.
Type error: This exception is thrown when the toFixed() method is called on an object that is not a type number.
The above is the detailed content of How to use toExponential method. For more information, please follow other related articles on the PHP Chinese website!