美文网首页
字节单位转换

字节单位转换

作者: ultimater | 来源:发表于2018-02-28 10:28 被阅读4次

    用于字节大小的转换

    bytesToSize(bytes) {
           if (bytes === 0) {
            return '0 B';
            }
    
            let k = 1024;
    
            let sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
            let i = Math.floor(Math.log(bytes) / Math.log(k));
            let num = (bytes / Math.pow(k, i)).toFixed(1) > Math.floor((bytes / Math.pow(k, i))) ?
                (bytes / Math.pow(k, i)).toFixed(1) : Math.floor((bytes / Math.pow(k, i)));
            return num + ' ' + sizes[i];
        }
    
    

    相关文章

      网友评论

          本文标题:字节单位转换

          本文链接:https://www.haomeiwen.com/subject/ophbxftx.html