#!/usr/bin/envpython
#-*-coding:utf-8-*-
"""智能转换bytes为kb/mb/gb/tb/pb...
"""
import math
def convertBytes(bytes,lst=['Bytes','KB','MB','GB','TB','PB']):
i=int(math.floor(#舍弃小数点,取小
math.log(bytes,1024)#求对数(对数:若a**b=N则b叫做以a为底N的对数)
))
ifi>=len(lst):
i=len(lst)-1
return('%.2f'+""+lst[i])%(bytes/math.pow(1024,i))
def main():
lst=['Bytes','KB','MB','GB','TB','PB']
bytes=input('Bytes:')
print (convertBytes(bytes,lst))
if__name__=='__main__':
main()
网友评论