经常用到的JS单位转换函数:

function format_size(bytes) {
if (bytes === 0) return ‘0 B’;
var k = 1024;
sizes = [‘B’,’KB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’, ‘EB’, ‘ZB’, ‘YB’];
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(2) + ‘ ‘ + sizes[i];
//toPrecision(3) 后面保留一位小数,如1.0GB //return (bytes / Math.pow(k, i)).toPrecision(3) + ‘ ‘ + sizes[i];
//bytes / Math.pow(1024, Math.floor(Math.log(bytes) / Math.log(1024)))
}

 

最后更新: 2018年02月02日 23:25

原始链接: http://tekin.yunnan.ws/JavaScript/201705/29-js-format-size.html

× 请我吃糖~
打赏二维码