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

How to use round method

不言
Release: 2020-09-14 15:12:22
Original
14481 people have browsed it

How to use the round method: The round method in JavaScript can be used to round a number to its nearest integer. If the decimal part of the number is greater than or equal to 5, it is rounded to the nearest nearest whole number. If the decimal part of the number is less than 5, it is rounded to the nearest nearest integer.

How to use round method

# Let’s look at the specific use of the round method.

First let’s take a look at the basic syntax of the round method

Math.round(x);
Copy after login

x represents the number that needs to be rounded.

Let’s look at specific examples

The code is as follows

<!DOCTYPE html>
<html>
   <head>
      <title></title>
   </head>
   <body> 
<script type="text/javascript"> 
    var value = Math.round( 0.5 );
         document.write("0.5四舍五入的值为:" + value ); 
         
         var value = Math.round( 20.7 );
         document.write("<br />20.7四舍五入的值为: " + value ); 
         
         var value = Math.round( 20.3 );
         document.write("<br />20.3四舍五入的值为:" + value ); 
         
         var value = Math.round( -20.3 );
         document.write("<br />-20.3四舍五入的值为:" + value ); 
</script> 
   </body>
</html>
Copy after login

The running results are as shown in the figure below

How to use round method

We can see that the numerical results of the above code are rounded to the nearest integer.

This article ends here. For more exciting content, you can pay attention to other column tutorials on the php Chinese website! ! !

The above is the detailed content of How to use round method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template