时间:2021-07-01 10:21:17 帮助过:15人阅读

下面我们来看round方法的具体使用方法。
首先我们来看一下round方法的基本语法
Math.round(x);
x表示需要四舍五入的数字。
我们来看具体示例
代码如下
<!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>运行结果如下图所示

我们可以看到上述代码的数字的结果都是四舍五入到最接近的整数。
本篇文章到这里就全部结束了,更多精彩内容大家可以关注Gxl网的其他栏目教程!!!
以上就是round方法怎么使用的详细内容,更多请关注Gxl网其它相关文章!