数字格式通过.toString()

时间:2013-12-10 04:56:05

标签: javascript number-formatting

新手问题:如何将numberWithCommas传递给我的值,以便我的数字格式正确?代码对我来说太复杂了。它正在工作,我只需要格式化数字。谢谢!

<script type="text/javascript">

function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

if(!localStorage.Val1){
    localStorage.Val1 = 1412015;
    localStorage.Val2 = 20000;
    localStorage.Val3 = 20000;
}

    setInterval( function() {
    var popcounter = document.getElementById('pop');
    var ethanolcounter = document.getElementById('ethanol');
    var meatlcounter = document.getElementById('meat');


localStorage.Val1 = parseInt(localStorage.Val1,10)+1;
      localStorage.Val2 = parseInt(localStorage.Val2,10)+1000;
      localStorage.Val3 = parseInt(localStorage.Val3,10)+1000;

      popcounter.innerHTML = localStorage.Val1,10;
      ethanolcounter.innerHTML = localStorage.Val2;
      meatlcounter.innerHTML = localStorage.Val3;

    },1000);

</script>

1 个答案:

答案 0 :(得分:0)

您问的是如何拨打JavaScript function?好吧,假设你的功能正常工作:

popcounter.innerHTML=numberWithCommas(localStorage.Val1)
//repeat for Val2 and Val3