aboutsummaryrefslogtreecommitdiff
path: root/steam/formatBytes.go
blob: 8627b58bc823f49acb1aab3f4b9c2545a2212095 (plain) (blame)
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
28
package steam

import (
	"fmt"
	"math"
)

func formatBytes(b int64) string {
	if b < 1024 {
		return fmt.Sprintf("%d b", b)
	}

	s := ""

	pfxs := "kmgt"
	for i := 0; i < len(pfxs); i++ {
		pow := math.Pow(float64(1024), float64(i+1))
		// This one is too big, return the previous string
		if b < int64(pow) {
			return s
		}
		s = fmt.Sprintf("%.2f %cb",
			float64(b)/(pow),
			pfxs[i])
	}

	return s
}