Skip to content

Instantly share code, notes, and snippets.

@cbrumelle
Created November 5, 2010 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbrumelle/664583 to your computer and use it in GitHub Desktop.
Save cbrumelle/664583 to your computer and use it in GitHub Desktop.
Format large numbers nicely for millions, billions and trillions
# 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