FormatDecimal formats bytes integer to human readable string according to SI international system of units. For example, 31323 bytes will return 31.32KB.
(value int64)
| 87 | // FormatDecimal formats bytes integer to human readable string according to SI international system of units. |
| 88 | // For example, 31323 bytes will return 31.32KB. |
| 89 | func (*Bytes) FormatDecimal(value int64) string { |
| 90 | multiple := "" |
| 91 | val := float64(value) |
| 92 | |
| 93 | switch { |
| 94 | case value >= EB: |
| 95 | val /= EB |
| 96 | multiple = "EB" |
| 97 | case value >= PB: |
| 98 | val /= PB |
| 99 | multiple = "PB" |
| 100 | case value >= TB: |
| 101 | val /= TB |
| 102 | multiple = "TB" |
| 103 | case value >= GB: |
| 104 | val /= GB |
| 105 | multiple = "GB" |
| 106 | case value >= MB: |
| 107 | val /= MB |
| 108 | multiple = "MB" |
| 109 | case value >= KB: |
| 110 | val /= KB |
| 111 | multiple = "KB" |
| 112 | case value == 0: |
| 113 | return "0" |
| 114 | default: |
| 115 | return strconv.FormatInt(value, 10) + "B" |
| 116 | } |
| 117 | |
| 118 | return fmt.Sprintf("%.2f%s", val, multiple) |
| 119 | } |
| 120 | |
| 121 | // Parse parses human readable bytes string to bytes integer. |
| 122 | // For example, 6GiB (6Gi is also valid) will return 6442450944, and |