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.