Skip to content

Instantly share code, notes, and snippets.

@hisplan
Created December 22, 2015 05:13
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 hisplan/18cfe5eb9dfb671adb0d to your computer and use it in GitHub Desktop.
Save hisplan/18cfe5eb9dfb671adb0d to your computer and use it in GitHub Desktop.
size in human format (K,M,G,T,P)
def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
print('the answer is %s' % human_format(7436313)) # prints 'the answer is 7.44M'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment