export function formatBytes(bytes: number): string { if (bytes === 0) return "0 B"; const k = 1000; // Use 1000 for "kB", use 1024 if you want "KiB" math const sizes = ["B", "kB", "MB", "GB", "TB", "PB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); const value = bytes / k ** i; // 1. toFixed(1) ensures we don't have crazy long decimals (1.33333 -> "1.3") // 2. parseFloat() strips useless zeros ("1.0" -> 1, "1.5" -> 1.5) // 3. i === 0 check ensures simple Bytes are always integers const formattedValue = i === 0 ? value : parseFloat(value.toFixed(1)); return `${formattedValue} ${sizes[i]}`; }