Math对象为数学常量和函数提供属性和方法。与其他全局对象不同,Math不是构造函数。Math的所有属性和方法都是静态的,可以通过将Math作为对象来调用,而无需创建它。本文主要介绍JavaScript(JS) Math.round( x ) 方法。

1、描述

返回一个数值四舍五入到最接近的整数。  

2、语法

它的语法如下:

Math.round( x )

3、返回值

返回舍入到最接近的整数的数字的值。

4、使用示例

<html>   
   <head>
      <title>JavaScript Math round() Method</title>
   </head>   
   <body>      
      <script type = "text/javascript">
         var value = Math.round( 0.5 );
         document.write("Math.round( 0.5 ) : " + value ); 
var value = Math.round( 20.7 ); document.write("<br />Math.round( 20.7 ) : " + value );
var value = Math.round( 20.3 ); document.write("<br />Math.round( 20.3 ) : " + value );
var value = Math.round( -20.3 ); document.write("<br />Math.round( -20.3 ) : " + value );
</script> </body> </html>

5、输出

Math.round( 0.5 ) : 1
Math.round( 20.7 ) : 21
Math.round( 20.3 ) : 20
Math.round( -20.3 ) : -20

推荐文档