Format the reputation
JavaScript (ES6), 76 68 bytes
x=>x<1e4?x.toLocaleString():(x<1e5?(x/1e2+.5|0)/10:(x/1e3+.5|0))+"k"
Another first attempt. Thank goodness for that handy .toLocaleString()
, the shortest alternative I could find is 21 bytes longer...
This separates thousands by either ,
or .
, depending on what country you live in. For five two bytes more, you can make it always use a comma:
x=>x<1e4?x.toLocaleString`en`:(x<1e5?(x/1e2+.5|0)/10:(x/1e3+.5|0))+"k"
Japt, 50 48 bytes
First attempt; there may be a better method.
U<A³?U:U<L²?Us i1', :(U<1e5?Ue2n)r /A:Ue3n)r)+'k
Try it online!
How it works
// Implicit: U = input integer, A = 10, L = 100
U<A³?U // If U is less than A³ (10³ = 1000), return U.
:U<L²? // Else, if U is less than L² (100² = 10000), return:
Us i1', // U.toString, with a comma inserted at position 1.
:( // Else, return:
U<1e5? // If U is less than 1e5:
Ue2n) // U * (10 to the power of -2),
r /A // rounded and divided by 10.
:Ue3n)r) // Else: U * (10 to the power of -3), rounded.
+'k // Either way, add a "k" to the end.
// Implicit: output last expression
JavaScript (ES6), 71
Beating @ETHProductions while he does not see my hint. He saw it.
x=>x<1e3?x:x<1e4?(x+='')[0]+','+x.slice(1):(x/1e3).toFixed(x<99950)+'k'
Test
f=x=>x<1e3?x:x<1e4?(x+='')[0]+','+x.slice(1):(x/1e3).toFixed(x<99950)+'k'
function test() { n=+I.value, O.textContent = n + ' -> ' + f(n) }
test()
<input id=I type=number value=19557 oninput=test()>
<pre id=O></pre>
Test