Javascript Math Rounding

Possible duplicate:
Is math javascript broken?

Ok, I have this script:

var x = new Date; setInterval(function() { $("#s").text((new Date - x) / 60000 + "Minutes Wasted"); }, 30000); 

It works great. Also, sometimes this gives me an answer like 4.000016666666666 . How can I get around this? If I have to rewrite the script, that's fine. Thanks!

+4
source share
4 answers

You can use the Math.floor () function to round

 $("#s").text(Math.floor((new Date - x) / 60000 + "Minutes Wasted")); 

or Math.ceil (), which is "round up"

 $("#s").text(Math.ceil((new Date - x) / 60000 + "Minutes Wasted")); 

or Math.round (), which are around or up or down, where it is closer to:

 $("#s").text(Math.round((new Date - x) / 60000 + "Minutes Wasted")); 
+14
source
 setInterval(function() { $("#s").text(parseInt((new Date - x) / 60000) + "Minutes Wasted"); }, 30000); 

use parseInt() .

0
source

If you are looking for a simple math round, then here is Math.round(40.45);

0
source

Source: https://habr.com/ru/post/1339834/


All Articles