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.哪里需要哪里搬

本文来自博客园,作者:danmo_xx,转载请注明原文链接:https://www.cnblogs.com/xx321/p/15904464.html

浙公网安备 33010602011771号