summaryrefslogtreecommitdiffstats
path: root/util/format.go
blob: fd6c71804e4eb685bb53185263e84a862f3462b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package util

import (
	"fmt"
)

func FormatFilesize(bytes int64) string {
	var unit string
	var value float64
	if bytes >= 1024*1024*1024*1024 {
		unit = "TiB"
		value = float64(bytes) / (1024 * 1024 * 1024 * 1024)
	} else if bytes >= 1024*1024*1024 {
		unit = "GiB"
		value = float64(bytes) / (1024 * 1024 * 1024)
	} else if bytes >= 1024*1024 {
		unit = "MiB"
		value = float64(bytes) / (1024 * 1024)
	} else if bytes >= 1024 {
		unit = "KiB"
		value = float64(bytes) / (1024)
	} else {
		unit = "B"
		value = float64(bytes)
	}
	return fmt.Sprintf("%.1f %s", value, unit)
}