FormatSizeUnit formats a number of bytes into a human-readable string.
(bytes uint64)
| 55 | |
| 56 | // FormatSizeUnit formats a number of bytes into a human-readable string. |
| 57 | func FormatSizeUnit(bytes uint64) string { |
| 58 | if bytes == 0 { |
| 59 | return "0 B" |
| 60 | } |
| 61 | |
| 62 | var result strings.Builder |
| 63 | for i := len(sizeUnitsS) - 1; i >= 0; i-- { |
| 64 | unit := sizeUnitsS[i] |
| 65 | if bytes >= unit.Factor { |
| 66 | value := float64(bytes) / float64(unit.Factor) |
| 67 | result.WriteString(fmt.Sprintf("%.2f %s", value, unit.Unit)) |
| 68 | return result.String() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // This should never be reached since we handle all sizes |
| 73 | return "0 B" |
| 74 | } |