4

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!

Community
  • 1
  • 1
0x60
  • 1,096
  • 4
  • 14
  • 23

4 Answers4

14

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"));
Mash
  • 1,339
  • 7
  • 8
2

Math.round?

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

Albireo
  • 10,977
  • 13
  • 62
  • 96
  • `Math.Round` _(sic)_ is not going to work; `Round` is not a constructor so doesn't start with a capital R. – Martijn Feb 15 '11 at 15:13
0
setInterval(function() {
    $("#s").text(parseInt((new Date - x) / 60000) + "Minutes Wasted");
}, 30000);

use parseInt().

powtac
  • 40,542
  • 28
  • 115
  • 170
0

If you're looking for simple Math round then here it is Math.round(40.45);

Umair A.
  • 6,690
  • 20
  • 83
  • 130