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

import "fmt"

func PrettyBytes(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)
}