summaryrefslogtreecommitdiff
path: root/common/pretty/bytesize.go
blob: c0bfcbcad4955d14e2c3c42d2c58f53093694627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package pretty

import "fmt"

func Bytes(b uint64) string {
	if b < 1000 {
		return fmt.Sprintf("%d Byte", b)
	}
	if b < 1000_000 {
		return fmt.Sprintf("%.2f KiB", float64(b)/1024)
	}
	if b < 1000_000_000 {
		return fmt.Sprintf("%.2f MiB", float64(b)/1024/1024)
	}
	if b < 1000_000_000_000 {
		return fmt.Sprintf("%.2f GiB", float64(b)/1024/1024/1024)
	}
	return fmt.Sprintf("%.2f TiB", float64(b)/1024/1024/1024/1024)
}