python⽶和厘⽶转换代码_Python字节单位转换实例from enum import Enum
class Values():
values={'B':1}
@staticmethod
def getValues():
if len(Values.values)<=1:
kbunits=['KB','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB']
kibunits=['KiBi','MiB','GiB','TiB','PiB','EiB','ZiB','YiB','BiB','NiB','DiB']
for index,unit in enumerate(kibunits):
Values.values[unit]=1<
for index,unit in enumerate(kbunits):
Values.values[unit]=10**((index+1)*3)
return Values.values
@staticmethod
python代码转换def get(key):
Values().get(key)
class Units(Enum):
def __new__(cls,name):
obj = object.__new__(cls)
print (name)
obj._value_=(name)
return obj
B=('B')
KB=('KB')
KiB=('KiBi')
MB=('MB')
MiB=('MiB')
GB=('GB')
GiB=('GiB')
TB=('TB')
TiB=('TiB')
PB=('PB')
PiB=('PiB')
EB=('EB')
EiB=('EiB')
ZB=('ZB')
ZiB=('ZiB')
YB=('YB')
YiB=('YiB')
BB=('BB')
BiB=('BiB')
NB=('NB')
NiB=('NiB')
DB=('DB')
DiB=('DiB')
class ByteUnitConversionUtil():
__defaultformat="%.5f"
@staticmethod
def convert(value,unit=Units.B,format=__defaultformat):
if(unit==Units.B):
return str(value).split(".",2)[0]+unit.name
else:
return (format % (value/unit.value))+unit.name
if __name__=="__main__":
vert(12313213453,Units.KiB,"%.2f"))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论