0

I've been trying to figure out how to insert commas into the result of this function for an hour now.

var i = 2574672248;
function increment2() {
    i++;
    document.getElementById('xxx').innerHTML = i;
}
setInterval('increment2()', 50);

The result is incremented by 1 every half a second. I'm trying to make it so, that if the result is 2394672248 for example, it's shown as "2,574,672,248" instead of the raw "2574672248". I've tried incorporating different comma inserting functions into it, but it's not working.

alex
  • 15
  • 1
  • 3
  • So you are trying to insert thousands separators? Is that right? – Matt Burland Sep 26 '14 at 18:45
  • What do thousands separators have to do with `setInterval`? Why did you post this code? Do you also want to do something else? – Artjom B. Sep 26 '14 at 18:46
  • Related: [How to print a number with commas as thousands separators in JavaScript](http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Ram Sep 26 '14 at 18:47
  • @alex: So maybe you should edit your question to say that. And remove the confusing mismatch of number `2394672248` is never going to give you `2,574,672,248` – Matt Burland Sep 26 '14 at 18:47

1 Answers1

2
var i = 2574672248;
function increment2() {
    i++;
    document.getElementById('xxx').innerHTML = i.toLocaleString('en');
}
setInterval('increment2()', 50);

From here: add commas to a number in jQuery

Community
  • 1
  • 1
ntzm
  • 4,663
  • 2
  • 31
  • 35