js根据文件大小自动判断单位KB、MB、GB

注意:这个方法最后的结果是保留两位小数,四舍五入 
校验转换的结果对不对,用你的数据除以1024
if (this.allDataList.totalStorage < 1024) {
        return this.allDataList.totalStorage + "B";
      } else if (
        this.allDataList.totalStorage >= 1024 &&
        this.allDataList.totalStorage < Math.pow(1024, 2)
      ) {
        return (
          parseFloat(this.allDataList.totalStorage / 1024).toFixed(2) + "KB"
        );
      } else if (
        this.allDataList.totalStorage >= Math.pow(1024, 2) &&
        this.allDataList.totalStorage < Math.pow(1024, 3)
      ) {
        return parseFloat(size / Math.pow(1024, 2)).toFixed(2) + "MB";
      } else if (this.allDataList.totalStorage > Math.pow(1024, 3)) {
        return (
          parseFloat(this.allDataList.totalStorage / Math.pow(1024, 3)).toFixed(
            2
          ) + "GB"
        );
      } else {
        return 0 + "B";
      }
实现方法:
1.在methods中定义一个方法,来实现(组件方式更方便)
2.具体看下方截图代码

 

 3.哪里需要哪里搬

 

 

posted @ 2022-02-17 14:18  danmo_xx  阅读(660)  评论(0编辑  收藏  举报