Webedelic
Webedelic

Reputation: 21

Comma between numbers script

I have tried endless solutions that I have found for this on these forums and none that I have found work or I am simply putting it in the wrong place. I am trying to force commas for thousand and millions places. Any suggestions and placement would be appreciated.

Thank you.

jQuery(window).scroll(startCounter);

function startCounter() {
    var hT = jQuery('.counter').offset().top,
        hH = jQuery('.counter').outerHeight(),
        wH = jQuery(window).height();
    if (jQuery(window).scrollTop() > hT+hH-wH) {
        jQuery(window).off("scroll", startCounter);
        jQuery('.counter').each(function () {
            var $this = jQuery(this);
            jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
                duration: 4000,
                easing: 'swing',
                step: function () {
                    $this.text(Math.ceil(this.Counter));
                }
            });
        });
    }
}

Upvotes: 1

Views: 57

Answers (1)

Miroslav Glamuzina
Miroslav Glamuzina

Reputation: 4557

Assuming you would want to comma seperate values by hunders,thousands,millions,...

You may do:

let num = 9876543210;

console.log(num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
// or
console.log((num).toLocaleString());
// or
console.log(new Intl.NumberFormat('en-US', {}).format(num));

Upvotes: 1

Related Questions