Math.ceil()
功能: 对一个数进行上取整。
功能: 对一个数进行上取整。
语法:
Math.ceil(x)
参数:
- x:一个数值。
返回值:
返回大于或等于
x
,并且与之最接近的整数。
注:
如果
x
是正数,则把小数
“
入
”
;如果
x
是负数,则把小数
“
舍
”
。
例:
<script type="text/javascript">
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
</script>
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
</script>
输出结果为:
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) );
2, 2, -1, -1
Math.floor()
功能:
对一个数进行下取整。
语法:
Math.floor(x)
参数:
- x:一个数值。
返回值:
返回小于或等于
x
,并且与之最接近的整数。
注:
如果
x
是正数,则把小数
“
舍
”
;如果
x
是负数,则把小数
“
入
”
。
例:
<script type="text/javascript">
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
</script>
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
</script>
输出结果为:
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) );
1, 1, -2, -2
Math.round()
功能:
四舍五入取整。
语法:
Math.round(x)
参数:
- x:一个数值。
返回值:
与
x
最接近的整数。
例:
<script type="text/javascript">
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) );
</script>
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) );
</script>
输出结果为:
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) );
1, 2, -1, -2
应用实例,自己做的标的详情页面有个满标时间倒计时器,具体代码如下,仅供参考
<script type="text/javascript">
$(function() {
setTimeout('showTime()', 1000);
});
function showTime() {
var endTime = $("#endTime").val();//满标时间
time_end = new Date(endTime);//结束的时间
var tt = time_end.getTime();
var now = new Date().getTime();
var cha = tt - now;
var SysSecond = parseInt(cha);
if (SysSecond > 0) {
int_day = Math.floor(SysSecond / 86400000);
SysSecond -= int_day * 86400000;
int_hour = Math.floor(SysSecond / 3600000);
SysSecond -= int_hour * 3600000;
int_minute = Math.floor(SysSecond / 60000);
SysSecond -= int_minute * 60000;
int_second = Math.floor(SysSecond / 1000);
if (int_hour < 10) {
int_hour = "0" + int_hour;
}
if (int_minute < 10) {
int_minute = "0" + int_minute;
}
if (int_second < 10) {
int_second = "0" + int_second;
}
$("#day").html(int_day);
$("#hours").html(int_hour);
$("#minutes").html(int_minute);
$("#seconds").html(int_second);
} else {
$("#day").html("00");
$("#hours").html("00");
$("#minutes").html("00");
$("#seconds").html("00");
}
setTimeout('showTime()', 1000);
}
</script>