准备好工具方法:
//参数bytes是文件大小字节数(整数) 参数precision是小数点后面的位数 function readableFileFize(bytes, precision) { if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-'; if (bytes <= 0) { return '0'; } if (typeof precision === 'undefined') precision = 1; var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'], number = Math.floor(Math.log(bytes) / Math.log(1024)); return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number]; }
使用方法
alert(readableFileFize(1024000, 2));
Python版本: https://blog.terrynow.com/2021/01/12/python-human-readable-file-size-with-precision/
Java版本:https://blog.terrynow.com/2021/01/10/java-human-readable-file-size/
文章评论