我有以下代码来帮助减少财产所欠的金额,但不是显示9-10位小数,我正在寻找它向下舍入到小数点后两位....
有人可以提供帮助吗?
<?php
$secondsleft = strtotime('Dec 31, 2014') - time();
$left = $secondsleft-1556952;
$owed = $left*0.0088192178;
$totalowed = $owed+267608.76;
?>
<script type="text/javascript">
window.onload = function () {
/* set your parameters(
number to countdown from,
pause between counts in milliseconds,
function to execute when finished
)
*/
startCountDown( <? php echo $totalowed; ?> , 1000, myFunction);
}
function startCountDown(i, p, f) {
// store parameters
var pause = p;
var fn = f;
var owed;
var owebb;
// make reference to div
var countDownObj = document.getElementById("countDown");
if (countDownObj == null) {
// error
alert("div not found, check your id");
// bail
return;
}
countDownObj.count = function (i) {
// write out count
countDownObj.innerHTML = i;
if (i == 0) {
// execute function
fn();
// stop
return;
}
setTimeout(function () {
// repeat
countDownObj.count(i - 0.009);
countDownObj.Math.round(countDownObj)
alert(countDownObj);
},
pause);
}
// set it going
countDownObj.count(i);
}
function myFunction() {
alert("Paid Off!");
}
</script>
答案 0 :(得分:0)
我在我的一个旧项目中使用了这个。基本上将UNIX时间转换为两个小数点“agos”...
var ago = val.modified_ago;
//
if (ago > 86400) {
ago = Math.round((ago / 86400) * 100) / 100 + "d";
} else if (ago > 3600) {
ago = Math.round((ago / 3600) * 100) / 100 + "h";
} else if (ago > 60) {
ago = Math.round((ago / 60) * 100) / 100 + "m";
} else {
ago = Math.round(ago * 100) / 100 + "s";
}
如果你不理解提示...
Math.round(num * 100) / 100 //for ROUNDING TO TWO DECIMAL POINTS!
答案 1 :(得分:0)
通过.toFixed()
方法将javascript浮点数/小数舍入到固定精度。
http://www.w3schools.com/jsref/jsref_tofixed.asp
e.g。
var someNumber = 1.2345678;
alert (someNumber.toFixed(2)); // outputs 1.23