Advertisement
Saijin_Naib

dataunits(x)

Apr 18th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. #Define a function to scale b/Kb/Mb/Gb/Tb/Pb/Eb/Zb/Yb/WTFb and return it rounded to two decimal places
  2. def dataunits(x):
  3.     try:
  4.         #def bytes(x):
  5.             if len(str(math.trunc(x))) <4 and len(str(math.trunc(x))) >= 1:
  6.                 return str(round(x,2)) + 'B'
  7.         #def kilobytes(x):
  8.             if len(str(math.trunc(x))) < 7 and len(str(math.trunc(x))) >= 4:
  9.                 return str(round(x/1000,2)) + 'KB'
  10.         #def megabytes(x):
  11.             if len(str(math.trunc(x))) < 10 and len(str(math.trunc(x))) >=7:
  12.                 return str(round(x/1000000,2)) + 'MB'
  13.         #def gigabytes(x):
  14.             if len(str(math.trunc(x))) < 13 and len(str(math.trunc(x))) >=10:
  15.                 return str(round(x/1000000000,2)) + 'GB'
  16.         #def terabytes(x):
  17.             if len(str(math.trunc(x))) < 16 and len(str(math.trunc(x))) >=13:
  18.                 return str(round(x/1000000000000,2)) + 'TB'
  19.         #def petabytes(x):
  20.             if len(str(math.trunc(x))) < 19 and len(str(math.trunc(x))) >=16:
  21.                 return str(round(x/1000000000000000,2)) + 'PB'
  22.         #def exabytes(x):
  23.             if len(str(math.trunc(x))) < 22 and len(str(math.trunc(x))) >=19:
  24.                 return str(round(x/1000000000000000000,2)) + 'EB'
  25.         #def zettabytes(x):
  26.             if len(str(math.trunc(x))) < 25 and len(str(math.trunc(x))) >=22:
  27.                 return str(round(x/1000000000000000000000,2)) + 'ZB'
  28.         #def yottabytes(x):
  29.             if len(str(math.trunc(x))) < 28 and len(str(math.trunc(x))) >=25:
  30.                 return str(round(x/1000000000000000000000000,2)) + 'YB'
  31.         #def whatthefuckabytes(x):
  32.             if len(str(math.trunc(x))) >= 28:
  33.                 return str(round(x/1000000000000000000000000000,2)) + 'WTFB'
  34.     except SyntaxError:
  35.         return 'NaN'
  36.     except NameError:
  37.         return 'NaN'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement