Merge pull request #313 from crazy-max/util-format-size

util: formatFileSize
This commit is contained in:
CrazyMax
2024-04-25 10:27:38 +02:00
committed by GitHub
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];
}
}