计算机进制转换小程序

背景:由于最近下载了很多数据集,基本都很大,但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]))

测试效果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值