Just for fun, check out Code Golf: Friendly Number Abbreviator
The shortest JS answer:
function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}
p.s. this is probably not the fastest or best solution.
Duplicate: Is there a way to round numbers into a reader friendly format? (e.g. $1.1k)