blob: d5d2aabf49d7901a305d1785a7753ea4fa3aeef5 (
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
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
}
|