FormatBytes outputs the given number of bytes "s" as a human-readable string, rounding to the nearest half within .01.
(s uint64)
| 100 | // FormatBytes outputs the given number of bytes "s" as a human-readable string, |
| 101 | // rounding to the nearest half within .01. |
| 102 | func FormatBytes(s uint64) string { |
| 103 | var e float64 |
| 104 | if s == 0 { |
| 105 | e = 0 |
| 106 | } else { |
| 107 | e = math.Floor(log(float64(s), 1000)) |
| 108 | } |
| 109 | |
| 110 | unit := uint64(math.Pow(1000, e)) |
| 111 | suffix := sizes[int(e)] |
| 112 | |
| 113 | return fmt.Sprintf("%s %s", |
| 114 | FormatBytesUnit(s, unit), suffix) |
| 115 | } |
| 116 | |
| 117 | // FormatBytesUnit outputs the given number of bytes "s" as a quantity of the |
| 118 | // given units "u" to the nearest half within .01. |