此方法返回一个数四舍五入为最接近的整数的值。
语法
Math.round( x ) ;
下面是参数的详细信息:
- x: 一个数字
返回值:
- 返回数字四舍五入为最接近的整数的值。
例子:
<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>
这将产生以下结果:
First Test Value : 1 Second Test Value : 21 Third Test Value : 20 Fourth Test Value : -20
最后
以上就是背后蚂蚁最近收集整理的关于JavaScript中用于四舍五入的Math.round()方法讲解的全部内容,更多相关JavaScript中用于四舍五入内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复