diff options
Diffstat (limited to 'cmd/web/formatBytes.go')
| -rw-r--r-- | cmd/web/formatBytes.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/cmd/web/formatBytes.go b/cmd/web/formatBytes.go new file mode 100644 index 0000000..d5d2aab --- /dev/null +++ b/cmd/web/formatBytes.go @@ -0,0 +1,29 @@ +package main + +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 +} + |
