【笔记】js的内存字节转化

时间:2023-03-09 15:05:40
【笔记】js的内存字节转化

function convertSize(size) {
                if(!size) {
                     return '0 Bytes';
               }
               var sizeNames = [' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
               var i = Math.floor(Math.log(size)/Math.log(1024));
               var p = (i > 1) ? 2 : 1;
               if(p==1){
                   return 1+sizeNames[1];
               }else{
                   return (size/Math.pow(1024, Math.floor(i))).toFixed(p) + sizeNames[i];
               }
       }