失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 转换 bytes 为 kb/mb/gb/tb/pb…

转换 bytes 为 kb/mb/gb/tb/pb…

时间:2023-05-21 13:00:27

相关推荐

转换 bytes 为 kb/mb/gb/tb/pb…

独角兽企业重金招聘Python工程师标准>>>

智能转换 bytes 为 kb/mb/gb/tb/pb… 用到了 math 模块中的一些函数

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

如果觉得《转换 bytes 为 kb/mb/gb/tb/pb…》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。