FormatBytes formats a byte count to a human-readable format with dynamic units Always shows 2 decimal places and chooses the most appropriate unit (GB, TB, PB).
(bytes int64)
| 39 | // FormatBytes formats a byte count to a human-readable format with dynamic units |
| 40 | // Always shows 2 decimal places and chooses the most appropriate unit (GB, TB, PB). |
| 41 | func FormatBytes(bytes int64) string { |
| 42 | const ( |
| 43 | GB = 1024 * 1024 * 1024 |
| 44 | TB = 1024 * GB |
| 45 | PB = 1024 * TB |
| 46 | ) |
| 47 | |
| 48 | bytesFloat := float64(bytes) |
| 49 | |
| 50 | switch { |
| 51 | case bytes >= PB: |
| 52 | return fmt.Sprintf("%.2f PB", bytesFloat/PB) |
| 53 | case bytes >= TB: |
| 54 | return fmt.Sprintf("%.2f TB", bytesFloat/TB) |
| 55 | case bytes >= GB: |
| 56 | return fmt.Sprintf("%.2f GB", bytesFloat/GB) |
| 57 | default: |
| 58 | // For values less than 1GB, still show in GB with decimals |
| 59 | return fmt.Sprintf("%.2f GB", bytesFloat/GB) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // FormatBytesFloat converts float64 GB values to human-readable format |
| 64 | // Input is assumed to be in GB, converts to appropriate units with 2 decimal places. |
no outgoing calls
no test coverage detected