0

I want to show numbers greater than one thousand with a comma, but I cannot because when I use number_format from the CountTo plugin, the number appears as NaN.

Here is my code (the code works; I just want to add the comma)

<div class="col-xs-6 col-sm-3">
    <div class="font-w700 text-gray-darker animated fadeIn">SickGen Users</div>
    <div class="text-muted animated fadeIn"><small><i class="si si-user"></i> Total</small></div>
    <a class="h2 font-w300 text-primary animated flipInX" data-toggle="countTo" data-to="<?php echo $totalusers;?>"></a>
</div>
<div class="col-xs-6 col-sm-3">
    <div class="font-w700 text-gray-darker animated fadeIn">Alts Database</div>
    <div class="text-muted animated fadeIn"><small><i class="si si-list"></i> Total</small></div>
    <a class="h2 font-w300 text-primary animated flipInX" data-toggle="countTo" data-to="<?php echo $totalalts;?>"></a>
</div>
<div class="col-xs-6 col-sm-3">
    <div class="font-w700 text-gray-darker animated fadeIn">Generators</div>
    <div class="text-muted animated fadeIn"><small><i class="fa fa-spinner"></i> Total</small></div>
    <a class="h2 font-w300 text-primary animated flipInX" data-toggle="countTo" data-to="<?php echo $totalgens;?>"></a>
</div>
<div class="col-xs-6 col-sm-3">
    <div class="font-w700 text-gray-darker animated fadeIn">Alts Generated</div>
    <div class="text-muted animated fadeIn"><small><i class="si si-refresh"></i> Total</small></div>
    <a class="h2 font-w300 text-primary animated flipInX" data-toggle="countTo" data-to="<?php echo $generated;?>"></a>
</div>

Here you can see how it looks now (of course you can't see the count effect)

Like show now

Here is what I want it to look like

Here like i want

Can you help me to add the comma?

script

 App.initHelper('appear-countTo');
     *
     */
    var uiHelperAppearCountTo = function(){
        // Init counter functionality
        jQuery('[data-toggle="countTo"]').each(function(){
            var $this       = jQuery(this);
            var $after      = $this.data('after');
            var $before     = $this.data('before');
            var $speed      = $this.data('speed') ? $this.data('speed') : 1500;
            var $interval   = $this.data('interval') ? $this.data('interval') : 15;

            $this.appear(function() {
                $this.countTo({
                    speed: $speed,
                    refreshInterval: $interval,
                    onComplete: function() {
                        if($after) {
                            $this.html($this.html() + $after);
                        } else if ($before) {
                            $this.html($before + $this.html());
                        }
                    }
                });
            });
        });
    };
Juan Montes
  • 33
  • 1
  • 7

1 Answers1

0

There's a formatter(value, options) configuration option in the documentation for countTo

$this.countTo({
  speed: $speed,
  refreshInterval: $interval,
  onComplete: function() {
    if ($after) {
      $this.html($this.html() + $after);
    } else if ($before) {
      $this.html($before + $this.html());
    }
  },
  formatter: function(value, options) {
    //default: return value.toFixed(options.decimals);
    return value.toFixed(options.decimals).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  }
});

The formatting code was taken from this StackOverflow answer and will work with any number that has 3 or less decimal places.

To show how it works:

function numberWithCommas(x) {
    return x.toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

console.log(numberWithCommas(13718))
J. Titus
  • 9,535
  • 1
  • 32
  • 45