Javascript Math Rounding
Possible Duplicate:
Is JavaScript's Math broken?
Okay, I have this script:
var x = new Date;
setInterval(function() {
$("#s").text((new Date - x) / 60000 + "Minutes Wasted");
}, 30000);
It works perfectly fine. Except it sometimes gives me an answer like 4.000016666666666
. How can I round that? If I have to rewrite the script, that is okay. Thanks!
You can use Math.floor() function to 'round down'
$("#s").text(Math.floor((new Date - x) / 60000 + "Minutes Wasted"));
or Math.ceil(), which 'round up'
$("#s").text(Math.ceil((new Date - x) / 60000 + "Minutes Wasted"));
or Math.round(), which round either up or down, where it's closer to:
$("#s").text(Math.round((new Date - x) / 60000 + "Minutes Wasted"));
Math.round
?
See http://www.w3schools.com/jsref/jsref_obj_math.asp
setInterval(function() {
$("#s").text(parseInt((new Date - x) / 60000) + "Minutes Wasted");
}, 30000);
使用parseInt()
。
上一篇: 如何解决在Java中的COS 90问题?
下一篇: Javascript数学舍入