Home >Web Front-end >JS Tutorial >How to use toFixed method

How to use toFixed method

不言
不言Original
2019-02-14 09:51:4116473browse

The toFixed() method in How to use toFixed method is used to format a number using fixed-point notation, formatting the number with a specific number of digits to the right of the decimal point. That is to say, Number can be rounded to a number with a specified number of decimal places. Let's take a look at the specific method of toFixed().

How to use toFixed method

First let’s take a look at the basic syntax of toFixed()

number.toFixed( num )

num represents the number of digits displayed after the decimal point.

If no parameters are specified in toFixed(), it will not display any numbers after the decimal places.

Let’s look at a specific example

There are no parameters in toFixed()

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var test=213.73145; 
    document.write(test.toFixed());           
</script> 
</body>
</html>

Running results It is: 214

##There are parameters in toFixed()

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var test=213.73145; 
    document.write(test.toFixed(3));           
</script> 
</body>
</html>

The running result is: 213.731

Use the toFixed() method where the number is in exponential form: The toFixed() function can be used to convert an exponential number to have a specific number of decimal places.

The code is as follows

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
<script type="text/javascript"> 
    var test=2.13e+15; 
    document.write(test.toFixed(2));           
</script> 
</body>
</html>

The running result is: 213000000000000.00

This article ends here. For more exciting content, you can follow php Tutorials on other related columns on the Chinese website! ! !

The above is the detailed content of How to use toFixed 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