Created
January 9, 2014 22:09
-
-
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.)
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
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