-1
<script type="text/javascript">
  var m$ = jQuery.noConflict();
  m$(document).ready(function(){
    num =  623000;
    prev = 623556;  
    subtract = num - prev;
    subtract /= 24;
    subtract /= 60;
    subtract /= 60; 

    var timerID = setInterval(function() {
      if(num > 0){
    subtract *= 1000;
    subtract = Math.round(subtract);
    subtract /= 1000;
    num -= subtract;
    num *= 10000;
    num /= 10000;
    num = Math.round(num).toFixed(3);
    m$('.dynamic').html(addCommas(num));
      }
      else {
        clearInterval(timerID);
      }
    }, 1000 );
});

function addCommas(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}
</script>

It a ticker that updates every second by subtracting the variable subtract from num. Unfortunately it no longer ticks down. I had it ticking just fine when I wasn't trying to do toFixed() to keep the zeros in the decimal place.

I Googled this and it said that I should use a string by doing the following:

numstr = Math.round(num + "").toFixed(3);

And that didn't work either, at one point I was getting NaN.

  • what `Math.round(num)` returns – xkeshav Feb 10 '12 at 06:36
  • 1
    I can't see how adding a string inside `Math.round()` would help. Can you please give an example of what the first three or four numbers would be when the code runs? It is not obvious what your code is supposed to be doing. I mean, obviously it is calculating some value every second, but what is the point of the calculation? – nnnnnn Feb 10 '12 at 06:49
  • I would recommend reading about [Number.toFixed()](https://developer.mozilla.org/en/Javascript/Reference/Global_Objects/Number/toFixed) and [Math.round()](https://developer.mozilla.org/en/Javascript/Reference/Global_Objects/Math/round). Your explanation and code sample alone are not clear enough for me to understand what you're actually trying to do, but I am sure that you're making it more complicated that it needs to be! – Sameera Thilakasiri Feb 10 '12 at 06:36
  • Take a look at [`number_format` from PHPJS](http://phpjs.org/functions/number_format:481). You can use it to round a number to a set number of decimal points, and add thousand separators (instead of your `addCommas` function). – Niet the Dark Absol Feb 10 '12 at 06:34

1 Answers1

1

Your num is 623000 and subtract is only -0.006, while you are rounding num in your loop. So, what do you expect? Math.round(623000 - (-0.006)) = 623000 and it always would be the same number.

And if you want to prefix zeroes then you should do it in the output, without update of the variable that keeps the numerical value.

ps: what is the purpose of

num *= 10000;
num /= 10000;

? Probably num = Math.round(num) should be between them and this is the reason of your problems?

and you could use this function to pad a number with leading zeroes

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }

    return str;
}

or check this topic How to output integers with leading zeros in JavaScript

Community
  • 1
  • 1
Cheery
  • 16,063
  • 42
  • 57