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];
}
posted @ 2018-06-22 18:08  seesun2012  阅读(723)  评论(0编辑  收藏  举报