JavaScript中用于四舍五入的Math.round()方法讲解
内容摘要
此方法返回一个数四舍五入为最接近的整数的值。
语法
Math.round( x ) ;
下面是参数的详细信息:
x: 一个数字
返回值:
返回数字四舍五入为最接近的整数的值。
语法
Math.round( x ) ;
下面是参数的详细信息:
x: 一个数字
返回值:
返回数字四舍五入为最接近的整数的值。
文章正文
此方法返回一个数四舍五入为最接近的整数的值。
语法
1 | Math. round ( x ) ; |
下面是参数的详细信息:
- x: 一个数字
返回值:
- 返回数字四舍五入为最接近的整数的值。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <html> <head> <title>JavaScript Math round () Method</title> </head> <body> <script type= "text/javascript" > var value = Math. round ( 0.5 ); document.write( "First Test Value : " + value ); var value = Math. round ( 20.7 ); document.write( "<br />Second Test Value : " + value ); var value = Math. round ( 20.3 ); document.write( "<br />Third Test Value : " + value ); var value = Math. round ( -20.3 ); document.write( "<br />Fourth Test Value : " + value ); </script> </body> </html> |
这将产生以下结果:
1 2 3 4 | First Test Value : 1 Second Test Value : 21 Third Test Value : 20 Fourth Test Value : -20 |
代码注释