更改字节转换方法
This commit is contained in:
@@ -197,15 +197,17 @@ export function CountDown(time: number) {
|
|||||||
/**
|
/**
|
||||||
* 字节转换为可读格式
|
* 字节转换为可读格式
|
||||||
* @param {number} bytes - 字节数
|
* @param {number} bytes - 字节数
|
||||||
* @param {number} decimals - 保留小数位数,默认2位
|
* @param {number} options - 选项对象
|
||||||
|
* @param {number} options.decimals - 保留小数位数,默认2位
|
||||||
|
* @param {boolean} options.unitBig - 是否使用大写单位,默认false
|
||||||
* @returns {string} 格式化后的字符串
|
* @returns {string} 格式化后的字符串
|
||||||
*/
|
*/
|
||||||
export function FormatBytes(bytes, decimals = 2) {
|
export function FormatBytes(bytes, options: { decimals?: number, unitBig?: boolean } = {}) {
|
||||||
if (bytes === 0) return '0 B';
|
|
||||||
if (!bytes || isNaN(bytes)) return '0 B';
|
if (!bytes || isNaN(bytes)) return '0 B';
|
||||||
|
const { decimals = 2, unitBig = false } = options;
|
||||||
const k = 1024;
|
const k = 1024;
|
||||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
const sizes = unitBig ? ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
const value = bytes / Math.pow(k, i);
|
const value = bytes / Math.pow(k, i);
|
||||||
return `${Number(value.toFixed(decimals))} ${sizes[i]}`;
|
return `${Number(value.toFixed(decimals))} ${sizes[i]}`;
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@
|
|||||||
})
|
})
|
||||||
const allTotalSize = computed(() => {
|
const allTotalSize = computed(() => {
|
||||||
const total = list.value.reduce((pre, cur) => pre + cur.fileSize, 0)
|
const total = list.value.reduce((pre, cur) => pre + cur.fileSize, 0)
|
||||||
const str = FormatBytes(total)
|
const str = FormatBytes(total, { unitBig: true })
|
||||||
return {
|
return {
|
||||||
size: str.split(' ')[0],
|
size: str.split(' ')[0],
|
||||||
unit: str.split(' ')[1]
|
unit: str.split(' ')[1]
|
||||||
|
|||||||
Reference in New Issue
Block a user