4

UPDATE

For JavaScript, see CMS's implementation below. It is much more elegant than the one I provide in the body of this Q.


// formats a number similar to the way stack exchange sites // format reputation. e.g. // for numbers< 10000 the output is '9,999' // for numbers > 10000 the output is '10k' with one decimal place when needed function getRepString(rep) { var repString;

    if (rep < 1000)
    {
        repString = rep;
    }
    else if (rep < 10000)
    {
        var mod = rep % 1000;

        repString = ((rep - mod) / 1000)
        + ","
        + ('000' + mod.toString()).slice(-3);
    }
    else
    {
        repString = (rep / 1000).toFixed(1).replace(".0", "") + "k";
    }

    return repString.toString();
}

Output:

  • getRepString(999) == '999'
  • getRepString(1000) == '1,000'
  • getRepString(9999) == '9,999'
  • getRepString(10000) == '10k'
  • getRepString(10100) == '10.1k'

Post an implementation in the language of your choice.

RegDwight
  • 101
  • 1
  • 1
  • 5
Sky Sanders
  • 12,068
  • 3
  • 31
  • 60
  • a more elegant implementation is heading our way... i hope. – Sky Sanders Jul 05 '10 at 09:24
  • You mean one that we don't have to manually do? Surely thats the whole point of giving us the "raw" score? We can format it how we like. – JonB Jul 05 '10 at 10:20
  • @jonb - no, i mean that someone showed me a more elegant way to do this and i encouraged him to post it here and hope he does, otherwise I am going to have to post it myself. and, yes, raw data is appropriate for an api return, but i as well as others, in various languages, wish to present this information in a more friendly format. that is what this post is about. – Sky Sanders Jul 05 '10 at 15:50
  • It looks like most of the implementations here are slightly off. Numbers like 10999 would show as 11k. Also 12452 would be 12.5k. SO rounds up. – jjnguy Aug 14 '10 at 22:14
  • @jjn - so what you are saying is that the examples shown truncate instead of round up as SO does? I can't speak for any but http://stackapps.com/questions/1012/how-to-format-reputation-numbers-similar-to-stack-exchange-sites/1386#1386 which performs as desired. – Sky Sanders Aug 14 '10 at 22:37
  • @Code, Yeah. I didn't test out any of them, but it looks liek a bunch of them truncate instead of rounding. – jjnguy Aug 14 '10 at 23:03

8 Answers8

5

Here you go, another JavaScript approach, originally posted on SO:

function getRepString (rep) {

  rep = rep+''; // coerce to string

  if (rep < 1000) { // return the same number
    return rep; 
  }

  if (rep < 10000) { // place a comma between

    return rep.charAt(0) + ',' + rep.substring(1);
  } 

  // divide and format
  return (rep/1000).toFixed(rep % 1000 != 0)+'k';

}

Check the output results here.

  • 3
  • for you. this is the way to do it in JS. I have removed the JS tag, so there is no 'right' answer now, just an x-language implementation free for all. Thanks for schooling me.
  • – Sky Sanders Jul 05 '10 at 17:00
  • Using this one now, had a convoluted algorithm for the rep < 10000 case. – Igor Zevaka Jul 07 '10 at 02:39