Home  >  Article  >  Web Front-end  >  How to use toExponential method

How to use toExponential method

不言
不言Original
2019-02-19 16:35:203332browse

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().

How to use toExponential method

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn