js格式化文件大小,单位:Bytes、KB、MB、GB

原文出自:https://blog.csdn.net/seesun2012

//  格式化文件大小
function renderSize(value){
    if(null==value||value==''){
        return "0 Bytes";
    }
    var unitArr = new Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB");
    var index=0,
        srcsize = parseFloat(value);
 index=Math.floor(Math.log(srcsize)/Math.log(1024));
    var size =srcsize/Math.pow(1024,index);
    //  保留的小数位数
    size=size.toFixed(2);
    return size+unitArr[index];
}
原文地址:https://www.cnblogs.com/seesun2012/p/9214775.html