etheryo/src/lib/ts/size.ts

16 lines
No EOL
374 B
TypeScript

export function get_size(kbytes: number): string {
if (kbytes < 1024) {
return `${kbytes} KiB`;
}
const units = ["MiB", "GiB", "TiB"];
let size = kbytes / 1024;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${Math.round(size)} ${units[unitIndex]}`;
}