JavaScript中用于四舍五入的Math.round()方法讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JavaScript中用于四舍五入的Math.round()方法讲解

  2020-05-14 我要评论

 此方法返回一个数四舍五入为最接近的整数的值。
语法

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 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们