修改计数计时器以在Javascript中包含货币格式

时间:2018-02-08 03:09:34

标签: javascript jquery

我有一个简单的计数器,它取一个起始编号并随机计数。但是,它在包含小数和逗号的数字上无法正常工作。

如何将其修改为以美分正确计算?

例如,我想从数字1,234,567.15开始计算,每500毫秒,它会在最后添加一个随机数量的美分。然后,当它超过99时,它会增加美元价值。

当前用法:

<span>$</span><span id="counter">1,234,567.15</span>

使用Javascript:

<script>
var timer;
function startCount() {
    timer = setInterval(count, 500); // 200 = 200ms delay between counter changes. Lower num = faster, Bigger = slower.
}
function count() {
    var do_wait = Math.ceil(4 * Math.random());
    if (do_wait == 4) {
        var rand_no = Math.ceil(4 * Math.random()); // 4 = random decrement amount. Counter will decrease anywhere from 1 - 4.
        var el = document.getElementById('counter');
        var currentNumber = parseFloat(el.innerHTML);
        var newNumber = currentNumber + rand_no;
        if (newNumber < 99999999999) {
            el.innerHTML = newNumber;
        }
    }
}
startCount(); 
</script>

1 个答案:

答案 0 :(得分:0)

parseFloat不会处理',',它会停止解析会遇到',';

最好的方法是使用newNumber.toLocaleString()

var timer;

function startCount() {
    timer = setInterval(count, 500); // 200 = 200ms delay between counter changes. Lower num = faster, Bigger = slower.
}

function count() {
    var do_wait = Math.ceil(4 * Math.random());
    if (do_wait == 4) {
        //  var rand_no = Math.ceil(4 * Math.random()); // 4 = random decrement amount. Counter will decrease anywhere from 1 - 4.
      var rand_no = 4 * Math.random(); //if you also want cents change,take this and comment previous line
        var el = document.getElementById('counter');
        var currentNumber = parseFloat(el.innerHTML.replace(/,/g, ''));
        var newNumber = currentNumber + rand_no;
        if (newNumber < 99999999999) {
            el.innerHTML = Number(newNumber.toFixed(2)).toLocaleString();
        }
    }
}
startCount();
<span>$</span><span id="counter">1,234,567.15</span>

如果不起作用,请手动处理:

将','替换为''并计算数字并添加','返回:

var timer;

function startCount() {
  timer = setInterval(count, 500); // 200 = 200ms delay between counter changes. Lower num = faster, Bigger = slower.
}

function count() {
  var do_wait = Math.ceil(4 * Math.random());
  if (do_wait == 4) {
    // var rand_no = Math.ceil(4 * Math.random()); // 4 = random decrement amount. Counter will decrease anywhere from 1 - 4.
    var rand_no = 4 * Math.random(); //if you also want cents change,take this and comment previous line    
    var el = document.getElementById('counter');
    var currentNumber = parseFloat(el.innerHTML.replace(/,/g, ''));
    var newNumber = currentNumber + rand_no;
    if (newNumber < 99999999999) {
      el.innerHTML = processNumber(newNumber);
    }
  }

  function processNumber(num) {
    let strArr = num.toFixed(2).split('.')[0].split(/|/g).reverse();
    let result = strArr.map(function(v, index) {
      return index % 3 === 0 ? v + ',' : v;
    }).reverse();

    return result.join('').replace(/,$/, '.') + num.toFixed(2).split('.')[1];
  }
}
startCount();
<span>$</span><span id="counter">1,234,567.15</span>