diff --git a/src/utils/parse-utils.ts b/src/utils/parse-utils.ts index 327204ae..b04a2cad 100644 --- a/src/utils/parse-utils.ts +++ b/src/utils/parse-utils.ts @@ -154,21 +154,12 @@ export function parseSizeFromKbs(sizeByte: number, timeInterval: number): any { * @param bits 字节Bit大小 * @returns MB */ -export function parseSizeFromBytes(bits: number | string): string { +export function parseSizeFromBits(bits: number | string): string { bits = Number(bits) || 0; - const kb = bits / 1024; - const mb = kb / 1024; - const gb = mb / 1024; - const tb = gb / 1024; - - if (tb >= 1) { - return tb.toFixed(2) + ' TB'; - } else if (gb >= 1) { - return gb.toFixed(2) + ' GB'; - } else if (mb >= 1) { - return mb.toFixed(2) + ' MB'; - } else if (kb >= 1) { - return kb.toFixed(2) + ' KB'; - } - return bits + ' B'; + if (bits <= 0) return '0 B'; + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + const unitIndex = Math.floor(Math.log2(bits) / 10); + const value = (bits / Math.pow(1024, unitIndex)).toFixed(2); + const unti = units[unitIndex]; + return `${value} ${unti}`; } diff --git a/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts b/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts index e0da6a4b..4266cfa8 100644 --- a/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts +++ b/src/views/dashboard/overview/hooks/useUPFTotalFlow.ts @@ -1,4 +1,4 @@ -import { parseSizeFromBytes } from '@/utils/parse-utils'; +import { parseSizeFromBits } from '@/utils/parse-utils'; import { ref } from 'vue'; type TFType = { @@ -33,8 +33,8 @@ export const upfTotalFlow = ref([ /**UPF-总流量数 数据解析 */ export function upfTFParse(data: Record) { let { up, down } = data; - up = parseSizeFromBytes(up); - down = parseSizeFromBytes(down); + up = parseSizeFromBits(up); + down = parseSizeFromBits(down); return { up, down }; }