9 lines
291 B
TypeScript
9 lines
291 B
TypeScript
export function formatSize(bytes: number): string {
|
|
if (bytes <= 0) return "";
|
|
const units = ["B", "KB", "MB", "GB"];
|
|
let i = 0;
|
|
let n = bytes;
|
|
for (; n >= 1024 && i < units.length - 1; i++) n /= 1024;
|
|
return `${n >= 10 || i === 0 ? Math.round(n) : n.toFixed(1)} ${units[i]}`;
|
|
}
|