views:

171

answers:

3

I want to convert a number into a string representation with a format similar to Stack Overflow reputation display.

e.g.

  • 999 == '999'
  • 1000 == '1,000'
  • 9999 == '9,999'
  • 10000 == '10k'
  • 10100 == '10.1k'
+8  A: 

UPDATE: CMS got the check and provides a superior answer. Send any more votes his way.

// 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)
    {
        // removed my rube goldberg contraption and lifted
        // CMS version of this segment
        repString = rep.charAt(0) + ',' + rep.substring(1);
    }
    else
    {
        repString = (Math.round((rep / 1000) * 10) / 10) + "k"
    }

    return repString.toString();
}

Output:

  • getRepString(999) == '999'
  • getRepString(1000) == '1,000'
  • getRepString(9999) == '9,999'
  • getRepString(10000) == '10k'
  • getRepString(10100) == '10.1k'
Sky Sanders
I'm a little puzzled. You're asking a question and the next minute you're the one answering it?
the_void
@the it's called a 'how-to' post. say you just spent some time figuring out how to do something that you *couldn't* find on the internet. would it have saved you time if someone else had already posted some tested code? do you like to have your time saved? ;-) I do. I didn't actually search to complete this task, but someone mentioned that perhaps it would go over well on SO. looks like they were right. cheers. p.s. it is done all the time and is accepted practice. not usually by me, though.
Sky Sanders
If you say this is a 'how-to' post, then it should be community wiki - otherwise it seems like a rep-farm to me.
Jeriko
I guess that for this sort of thing you could have used the `community wiki` feature ;)
the_void
+4  A: 

Another approach that produces exactly the desired output:

function getRepString (rep) {
  rep = rep+''; // coerce to string
  if (rep < 1000) {
    return rep; // return the same number
  } else if (rep < 10000) { // place a comma between
    return rep.charAt(0) + ',' + rep.substring(1);
  } else { // divide and format
    return (rep/1000).toFixed(rep % 1000 != 0)+'k';
  }
}

Check the output results here.

CMS
actually, the toFixed was how i initially had implemented the >=10k but the `replace` made me feel dirty ;-) but the <10k is actually way too obvious for me to have come up with. thanks for that. you get the check, but i get to steal the <10k.
Sky Sanders
@code: Yeah, I felt dirty also, replace removed.
CMS
A: 

divide by 1000 then if result is greater than 1 round the number and concantenate a "k" on the end.

If the result is less than 1 just output the actual result!

liamfriel
if the number is less than 10000, it should be formatted with a separator, if greater, it should be formatted with a kilo indicator and 1 decimal place, if necessary. you are missing some of the details, liam.
Sky Sanders