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