Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created October 17, 2011 12:51
Show Gist options
  • Save KrofDrakula/1292541 to your computer and use it in GitHub Desktop.
Save KrofDrakula/1292541 to your computer and use it in GitHub Desktop.
A utility function for humanizing numbers
humanize = (value, precision = 2) ->
if value < 1e3
return value.toString()
if value < 1e6
return (value / 1e3).toPrecision(precision) + 'k'
if value < 1e9
return (value / 1e6).toPrecision(precision) + 'M'
if value < 1e12
return (value / 1e9).toPrecision(precision) + 'G'
(value / 1e12).toPrecision(precision) + 'T'
# Sample outputs:
# humanize 1048576, 3 # => "1.05M"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment