Created
November 5, 2010 18:37
-
-
Save cbrumelle/664583 to your computer and use it in GitHub Desktop.
Format large numbers nicely for millions, billions and trillions
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
# Format large numbers nicely for Millions, Billions and Trillions | |
# Example: | |
# humanize_large_number(123456789) => 123.46M | |
# humanize_large_number(123) => 123 | |
def humanize_large_number(i) | |
i = Integer(i) | |
case i | |
when 1000000..999999999 | |
"%.2fM" % (i/1000000.0) | |
when 1000000000..999999999999 | |
"%.2fB" % (i/1000000000.0) | |
when 1000000000000 | |
"%.2fT" % (i/1000000000000.0) | |
else i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment