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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With