From d762cdbae06efd194ffac7b976c16aac21a26f94 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Fri, 8 Jan 2021 23:03:01 -0500 Subject: Also show the size of games on the page listing --- steam/formatBytes.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 steam/formatBytes.go (limited to 'steam/formatBytes.go') diff --git a/steam/formatBytes.go b/steam/formatBytes.go new file mode 100644 index 0000000..8627b58 --- /dev/null +++ b/steam/formatBytes.go @@ -0,0 +1,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 +} -- cgit v1.2.3