背景:由于最近下载了很多数据集,基本都很大,但windows上都是以kb为单位进行显示,脑子有时无法快速估计数据集所栈内存,所以写了个简单的小程序进行转换。
计算机进制转换规则:
转换规则:
bitX8=byte
1KB (Kilobyte 千字节)=1024B,
1MB (Megabyte 兆字节 简称“兆”)=1024KB,
1GB (Gigabyte 吉字节 又称“千兆”)=1024MB,
1TB (Trillionbyte 万亿字节 太字节)=1024GB,其中1024=2^10 ( 2 的10次方),
1PB(Petabyte 千万亿字节 拍字节)=1024TB,
1EB(Exabyte 百亿亿字节 艾字节)=1024PB,
1ZB (Zettabyte 十万亿亿字节 泽字节)= 1024 EB,
1YB (Yottabyte 一亿亿亿字节 尧字节)= 1024 ZB,
1BB (Brontobyte 一千亿亿亿字节)= 1024 YB.
实现程序:
"""
author:wsy
data:2022-6-13
target:Conversion between computer unit base
"""
if __name__=="__main__":
base=['bit','Byte','KB','MB','GB','TB','PB','EB','ZB','BB']
while True:
c=input("停止程序输入-1,否则输入其他:")
if c=="-1":
break;
from_num=float(input("请输入原始数量(可为小数):"))
from_base=int(input("请输入原始单位对应的编号(0(bit),1(Byte),2(KB),3(MB),4(GB),5(TB), 6(PB), 7(EB),8(ZB),9(BB)):"))
to_base=int(input("请输入想要转换的单位编号(编号对应同上):"))
if from_base==to_base:
print(str(from_num)+str(base[from_base])+"=",str(from_num)+str(base[to_base]))
elif from_base<to_base:
tmp=from_base
target_num=from_num
if from_base==0:
target_num/=8
tmp+=1
while tmp<to_base:
target_num/=1024
tmp+=1
print(str(from_num)+str(base[from_base])+"="+str(target_num)+str(base[to_base]))
elif from_base>to_base:
tmp=from_base
target_num=from_num
while tmp>to_base and tmp>1:
target_num*=1024
tmp-=1
if to_base==0:
target_num*=8
print(str(from_num)+str(base[from_base])+"="+str(target_num)+str(base[to_base]))
测试效果:
更多推荐
计算机进制转换小程序
发布评论