fix: 字节转换工具

This commit is contained in:
TsMask
2024-02-01 09:43:50 +08:00
parent c10ad8c7b1
commit 8a590a8d3b
2 changed files with 10 additions and 19 deletions

View File

@@ -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}`;
}