(bytes: number, decimals: number = 1)
| 4 | * @returns Formatted string with appropriate units (B, KB, MB, GB) |
| 5 | */ |
| 6 | export function formatBytes(bytes: number, decimals: number = 1): string { |
| 7 | if (bytes === 0) return '0 B'; |
| 8 | |
| 9 | const k = 1024; |
| 10 | const sizes = ['B', 'KB', 'MB', 'GB']; |
| 11 | |
| 12 | const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 13 | const value = bytes / Math.pow(k, i); |
| 14 | |
| 15 | return `${value.toFixed(decimals)} ${sizes[i]}`; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Format latency to human-readable format with appropriate units |