Man Down

Man Down

  • NA
  • 48
  • 3.2k

How to add comma to separate numbers - JavaScript

Feb 6 2018 3:55 AM
I am trying to print an number in JavaScript with commas as separators. but when i add comma it will give me Nan value For example, I want to show the number 170607 as "170,607". How would I go about doing this?
 
HTML :
  1. <div class="row about-stats stats block-1-4 block-m-1-2 block-mob-full" data-aos="fade-up">   
  2.     <div class="col-block stats__col ">    
  3.         <div class="stats__count">4</div>    
  4.    </div>    
  5.     <div class="col-block stats__col">    
  6.         <div class="stats__count">40</div>     
  7.     </div>    
  8.     <div class="col-block stats__col">    
  9.         <div class="stats__count">170607</div>           
  10.     </div>    
  11.     <div class="col-block stats__col">    
  12.         <div class="stats__count">3447</div>            
  13.     </div>     
  14. </div>  
Script:
  1. /* Stat Counter 
  2.  * ------------------------------------------------------ */  
  3.  var clStatCount = function() {     
  4.      var statSection = $(".about-stats"),  
  5.          stats = $(".stats__count");
  6.      statSection.waypoint({
  7.          handler: function(direction) {
  8.              if (direction === "down") {
  9.                  stats.each(function () {  
  10.                      var $this = $(this);
  11.                      $({ Counter: 0 }).animate({ Counter: $this.text() }, {  
  12.                          duration: 4000,  
  13.                          easing: 'swing',  
  14.                          step: function (curValue) {  
  15.                              $this.text(Math.ceil(curValue));  
  16.                          }  
  17.                      });  
  18.                  });
  19.              }
  20.              // trigger once only  
  21.              this.destroy();
  22.          },
  23.          offset: "90%"
  24.      });  
  25.  };

Answers (8)