Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Created January 9, 2014 22:09
Show Gist options
  • Save cameronp98/8343004 to your computer and use it in GitHub Desktop.
Save cameronp98/8343004 to your computer and use it in GitHub Desktop.
Generate suffixes for large numbers in Python (e.g. 1,000,000 = "1M", 10,000 = "10K" etc.)
from random import randint
from math import log10, floor
def human_format(num, ends=["", "K", "M", "B", "T"]):
# divides by 3 to separate into thousands (...000)
return ends[int(floor(log10(num))/3)]
if __name__ == '__main__':
for i in range(10):
x = randint(1,10**i)
print(x, human_format(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment