避免UPF字节数空值

This commit is contained in:
lai
2024-01-29 00:59:49 +08:00
parent 8e535aee35
commit 45c20e507a

View File

@@ -155,6 +155,9 @@ export function parseSizeFromKbs(sizeByte: number, timeInterval: number): any {
* @returns
*/
export function formatBytes(bytes: number): string {
if (bytes === 0) {
return '0 B'; // 处理 bytes 为 0 的情况
}
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
const unitIndex = Math.floor(Math.log2(bytes) / 10);
const result =(bytes / Math.pow(1024, unitIndex)).toFixed(2) + ' ' + units[unitIndex];