圆月山庄资源网 Design By www.vgjia.com
def bytes_to_human(n): symbols = ('K','M','G','T','P','E','Z','Y') prefix = {} for i,s in enumerate(symbols): prefix[s] = 1 << (i + 1) * 10 for s in reversed(symbols): if n >= prefix[s]: value = float(n) / prefix[s] return '%.1f%s' % (value,s) return '%sB' % n
python编写的储存单位转换代码(以字节(B)为单位)
def bytes(bytes): if bytes < 1024: #比特 bytes = str(round(bytes, 2)) + ' B' #字节 elif bytes >= 1024 and bytes < 1024 * 1024: bytes = str(round(bytes / 1024, 2)) + ' KB' #千字节 elif bytes >= 1024 * 1024 and bytes < 1024 * 1024 * 1024: bytes = str(round(bytes / 1024 / 1024, 2)) + ' MB' #兆字节 elif bytes >= 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024: bytes = str(round(bytes / 1024 / 1024 / 1024, 2)) + ' GB' #千兆字节 elif bytes >= 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024: bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024, 2)) + ' TB' #太字节 elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024: bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024, 2)) + ' PB' #拍字节 elif bytes >= 1024 * 1024 * 1024 * 1024 * 1024 * 1024 and bytes < 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024: bytes = str(round(bytes / 1024 / 1024 / 1024 / 1024 / 1024 /1024, 2)) + ' EB' #艾字节 return bytes if __name__ == '__main__': print('0:' + bytes(0)) print('1:' + bytes(1)) print('2:' + bytes(10)) print('3:' + bytes(100)) print('4:' + bytes(1000)) print('5:' + bytes(10000)) print('6:' + bytes(100000)) print('7:' + bytes(1000000)) print('8:' + bytes(10000000)) print('9:' + bytes(100000000)) print('10:' + bytes(1000000000)) print('11:' + bytes(10000000000)) print('12:' + bytes(100000000000)) print('13:' + bytes(1000000000000)) print('14:' + bytes(10000000000000)) print('15:' + bytes(100000000000000)) print('16:' + bytes(1000000000000000)) print('17:' + bytes(10000000000000000)) print('18:' + bytes(100000000000000000)) print('19:' + bytes(1000000000000000000)) print('20:' + bytes(10000000000000000000)) print('20:' + bytes(100000000000000000000)) print('20:' + bytes(1000000000000000000000))
测试:
"D:\Program Files\Python\Python36\python.exe" C:/Users/Jochen/PycharmProjects/mysite/bytes.py
0:0 B
1:1 B
2:10 B
3:100 B
4:1000 B
5:9.77 KB
6:97.66 KB
7:976.56 KB
8:9.54 MB
9:95.37 MB
10:953.67 MB
11:9.31 GB
12:93.13 GB
13:931.32 GB
14:9.09 TB
15:90.95 TB
16:909.49 TB
17:8.88 PB
18:88.82 PB
19:888.18 PB
20:8.67 EB
20:86.74 EB
20:867.36 EBProcess finished with exit code 0
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 群星《狐妖小红娘月红篇 电视剧原声带》[320K/MP3][233.61MB]
- 英雄联盟s14四强淘汰赛规则是什么 全球总决赛四强淘汰赛规则详解
- 英雄联盟s14四强赛怎么分组 S14全球总决赛四强赛分组规则详解
- 英雄联盟s14四强赛抽签规则是什么 S14全球总决赛四强抽签规则详解
- ButterQuartet-ScintillaEarlyItalianStringQuartets(DeLuxe)(2024)[24Bit-WAV]
- 草原最美情歌《迷醉女中音》2CD/DTS-ES[WAV]
- 爵士女伶何芸妮《靡靡之音》(香港版)[WAVCUE]
- 游戏中辱骂他人同样侵犯名誉权 一玩家被判道歉
- 老游戏远程共享申请失败 美国版权局拒绝豁免请愿
- 通过本地化支付解决方案,解锁150亿美元拉美和非洲游戏市场
- 群星《狐妖小红娘月红篇 电视剧原声带》[FLAC/分轨][574.68MB]
- 群星《红色冲浪板 电影配乐专辑》[320K/MP3][106.63MB]
- 罗艺恒《What Could've Been》[320K/MP3][50.62MB]
- 于台烟.1989-人间山水【银河唱片】【WAV+CUE】
- 杨克强.1992-特制的面具【恩华声视】【WAV+CUE】