Javascript数学舍入

0x6*_*x60 4 javascript math jquery

可能重复:
JavaScript的数学是否被破坏?

好的,我有这个脚本:

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

它工作得很好.除了它有时给我一个答案4.000016666666666.我该怎么办呢?如果我必须重写脚本,那没关系.谢谢!

Mas*_*ash 14

你可以使用Math.floor()函数'向下舍入'

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

或Math.ceil(),'向上'

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

或者Math.round(),向上或向下舍入,它更靠近:

$("#s").text(Math.round((new Date - x) / 60000 + "Minutes Wasted"));
Run Code Online (Sandbox Code Playgroud)