added enum to use base 2 or base 10 for calcs

This commit is contained in:
Rob Weber 2024-07-29 13:52:05 +00:00
parent c5ba4f356f
commit ea6b3d0c38

View file

@ -1,9 +1,14 @@
export function formatBytes(bytes: number, decimals = 2) { export enum UNIT_BASE {
BASE_2 = 1024,
BASE_10 = 1000
}
export function formatBytes(bytes: number, decimals = 2, base: UNIT_BASE = UNIT_BASE.BASE_2) {
if (bytes === 0) { if (bytes === 0) {
return '0 Bytes'; return '0 Bytes';
} }
const k = 1024; const k = base;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k)); const i = Math.floor(Math.log(bytes) / Math.log(k));