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

时间:2015-06-15    点击:120   

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

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 

jQuery使用drag效果实现自由拖拽div
javascript 常见功能汇总
简介JavaScript中的setHours()方法的使用
在JavaScript中使用开平方根的sqrt()方法
JavaScript中的Math.sin()方法使用详解
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved