Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Math Rounding [duplicate]

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!

like image 811
0x60 Avatar asked Dec 05 '22 23:12

0x60


2 Answers

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"));
like image 157
Mash Avatar answered Dec 20 '22 13:12

Mash


Math.round?

See http://www.w3schools.com/jsref/jsref_obj_math.asp

like image 43
Albireo Avatar answered Dec 20 '22 14:12

Albireo