js格式化文件大小

      const sizeUnit = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      function formatSize(fileSizeInBytes) {
        const sizeType = parseInt(
          Math.floor(Math.log(fileSizeInBytes) / Math.log(1024)).toString(),
        );
        console.log('sizeType', sizeType);
        const size = (fileSizeInBytes / Math.pow(1024, sizeType)).toFixed(2);
        return size + sizeUnit[sizeType];
      }

 

posted @ 2022-10-05 22:18  我是格鲁特  阅读(251)  评论(0编辑  收藏  举报