util: formatFileSize

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2024-04-23 15:33:51 +02:00
parent d44748420a
commit 7621606be3
2 changed files with 34 additions and 0 deletions

View File

@@ -166,4 +166,12 @@ export class Util {
throw new Error(`parseBool syntax error: ${str}`);
}
}
public static formatFileSize(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
}