Created
October 17, 2011 12:51
-
-
Save KrofDrakula/1292541 to your computer and use it in GitHub Desktop.
A utility function for humanizing numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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