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

  作者:bea

此方法返回一个数四舍五入为最接近的整数的值。 语法 Math.round( x ) ; 下面是参数的详细信息: x: 一个数字 返回值: 返回数字四舍五入为最接近的整数的值。 例子: <html><head><title>JavaScript Math round() Method</title></head><body><script type="text/javasc

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




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






有用  |  无用

猜你喜欢