0

I'm terrible when it comes to JS. I have a small script that I am using in a Wordpress installation for a continually updating counter. Here is the code I am using:

window.setInterval(function () {
   var countStart = 147000000000;

   var count = countStart + (new Date().getTime() - new Date('12/16/2015').getTime())/.900;

   jQuery("#counter2").text(count.toFixed(0));
}, 1);

How can I amend this to output the number with thousands separators? I am aware of the numerous threads on the topic of thousands separators, but cannot seem to make any suggestions work with what I have already.

Scarecrow
  • 4,057
  • 3
  • 30
  • 56
TimmehNZ
  • 91
  • 5
  • 2
    Possible duplicate of [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) – forgivenson Dec 17 '15 at 12:38
  • Possible duplicate of [add commas to a number in jQuery](http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery) – itamar Dec 17 '15 at 12:40

3 Answers3

0

There is the toLocaleString() method:

jQuery("#counter2").text(count.toFixed(0).toLocaleString('en'));

It will add the thousands separator, as it is specified for the locale. English uses comma as the thousands separator, and dot for decimal mark.

For example this

var count = 102012.123;
console.log(count.toLocaleString('en'));

yields: 102,012.123.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

try this:

function formatNumber(){
  var a = 1234567890;
  console.log( Number(a.toFixed(0)).toLocaleString())
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

So this was the answer that worked for me provided by a friend:

    window.setInterval(function () {
    var countStart = 147000000000;
    var myDate = new Date().getTime() - new Date('12/16/2015').getTime();
    var count = countStart + (myDate) / 0.900;
    var countSeparated = Number(count.toFixed(0)).toLocaleString();
  jQuery("#counter2").text(countSeparated);
}, 1);

For reasons beyond my abilities, the above two answers worked in a console but not in my script within Wordpress

TimmehNZ
  • 91
  • 5