-1

I have block displaying the number of following for a twitter handle. Works great. The only issue, is that there are 11,000+ followers, and the API doesn't throw in the comma. How would I incorporate JavaScript to format the number so it looks nice?

Here's the twitter code I'm using:

$(function(){
$.ajax({
   url: 'http://api.twitter.com/1/users/show.json',
   data: {screen_name: 'handle'},
   dataType: 'jsonp',
   success: function(data) {
        $('#followers').html(data.followers_count);
   }
});
});


Thanks to neiker for help with a solution!
function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }

    $.ajax({
       url: 'http://api.twitter.com/1/users/show.json',
       data: {screen_name: 'handle'},
       dataType: 'jsonp',
       success: function(data) {
            $('#followers').html(numberWithCommas(data.followers_count));
       }
    });
cfox
  • 431
  • 2
  • 6
  • 18
  • Check this: http://stackoverflow.com/questions/2901102/how-to-print-number-with-commas-as-thousands-separators-in-javascript – neiker Nov 23 '12 at 16:39
  • Yeah saw that a few days ago, tried to implement, but I don't know JavaScript well enough to get that to work. – cfox Nov 23 '12 at 16:41
  • 1
    Put the "numberWithCommas" declaration above your ajax call.. then, change this: $('#followers').html(data.followers_count); to this $('#followers').html(numberWithCommas(data.followers_count)); – neiker Nov 23 '12 at 17:24
  • Oh you're awesome! That was indeed the one way I didn't try to combine the functions... Thanks so much! – cfox Nov 23 '12 at 17:40

1 Answers1

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

    $.ajax({
       url: 'http://api.twitter.com/1/users/show.json',
       data: {screen_name: 'handle'},
       dataType: 'jsonp',
       success: function(data) {
            $('#followers').html(numberWithCommas(data.followers_count));
       }
    });
cfox
  • 431
  • 2
  • 6
  • 18