-1

I have a 5-digit number, like 10000, and I want to display it as 10k, as I'll eventually have 6 digits (I'm talking about Twitter counts, actually). I suppose I have to substring, but I'm not that used to JavaScript just yet.

Here's just about what I'm trying to use. It basically gets the count of the followers by JSON.

<script type="text/javascript">
    $(function() {
        $.ajax({
            url: 'http://api.twitter.com/1/users/show.json',
            data: {
                screen_name: 'lolsomuchcom'
            },
            dataType: 'jsonp',
            success: function(data) {
            $('#followers').html(data.followers_count);
                }
        });
    });
</script>
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
Satan
  • 77
  • 2
  • 8

2 Answers2

3

Try :

$('#followers').html(Math.floor(data.followers_count/1000) + 'K');
Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
2
$('#followers').html(data.followers_count.substring(0, data.followers_count.length - 3)); 

Demo: http://jsfiddle.net/ZWfPW/

Edit.. here's the literal code, just for you:

$(function() {
    $.ajax({
        url: 'http://api.twitter.com/1/users/show.json',
        data: {
            screen_name: 'lolsomuchcom'
        },
        dataType: 'jsonp',
        success: function(data) {
            // Ensure it's a string
            data.followers_count += '';
            $('#followers').html(data.followers_count.substring(0, data.followers_count.length - 3) + 'K');
        }
    });
});
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145