【问题标题】:Limiting decimal places behind counter限制计数器后面的小数位
【发布时间】:2013-10-20 23:47:36
【问题描述】:

您好,我正在尝试限制此计数器结果后面的小数位数。我试过 toFixed 但无法让它工作......我是新手,任何帮助将不胜感激。

谢谢!

约瑟夫

<script type="text/javascript">                         
var counttx= "";
var counterrx=setInterval(timerrx, 1000);
function timerrx()
{
 counttx = +counttx + .607028;
if (counttx < 0)
{
 clearInterval(counterrx);
 return;
}
document.getElementById("timerrx").innerHTML=counttx;
}
</script>

【问题讨论】:

    标签: javascript count counter tofixed


    【解决方案1】:

    toFixed 不起作用可能是因为您将 counttx 声明为字符串,请在最后尝试:

    document.getElementById("timerrx").innerHTML= Number(counttx).toFixed(2);
    

    【讨论】:

    • 查看+counttxcounttx = +counttx + .607028; 中的位置。这已经将其转换为数字。
    • 谢谢你们俩。很有帮助!
    【解决方案2】:

    .toFixed() 是正确的函数:

     document.getElementById("timerrx").innerHTML = counttx.toFixed(2);
    

    工作演示:http://jsfiddle.net/jfriend00/HdJWD/

    【讨论】:

    • 非常感谢@jfriend00! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多