比较简单,直接干货,说明写在工具方法的注释里了:
# bytes 文件大小,单位:字节数bytes # precision 小数点后位数,可以是0 def readable_file_size(bytes, precision): for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']: if abs(bytes) < 1024.0: return '%s %s' % (format(bytes, '.%df' % precision), unit) bytes /= 1024.0 return '%s %s' % (format(bytes, '.%df' % precision), 'Yi') # 测试 print(readable_file_size(102401, 2))
Java版本: https://blog.terrynow.com/2021/01/10/java-human-readable-file-size/
Javascript版本:https://blog.terrynow.com/2021/01/09/javascript-human-readable-file-size/
文章评论