diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:00:13 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:00:13 +0100 |
| commit | 285d29d9e34a906f11b15388c637739c0d630cf7 (patch) | |
| tree | c0e9df625e747879880e15893769fef0693829c3 /utils | |
| parent | 3d924b60339016201b56c6f31be73e1efa63f8b1 (diff) | |
Convert numbers to human readable
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/utils.go | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/utils/utils.go b/utils/utils.go index e314367..65461f4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,9 @@ package utils -import "io/ioutil" +import ( + "fmt" + "io/ioutil" +) func Slurp(what *string, path string) error { bytes, err := ioutil.ReadFile(path) @@ -11,3 +14,37 @@ func Slurp(what *string, path string) error { } return nil } + +func Abs(x int) int { + if x < 0 { + return -x + } else { + return x + } +} + +func Human(x int) string { + units := []string{"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} + + f := float32(x) + i := 0 + + for ; f >= 1000; i++ { + f /= 1000 + } + + return fmt.Sprintf("%d%s", int(f), units[i]) +} + +func HumanMebi(x int) string { + units := []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} + + f := float32(x) + i := 0 + + for ; f >= 1024; i++ { + f /= 1024 + } + + return fmt.Sprintf("%d%s", int(f), units[i]) +} |
