From 77f728fb44a2dc2688620e589171b0a5e6bdd9e4 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Wed, 8 Aug 2018 22:30:24 -0700 Subject: [PATCH] Fix byte conversions; add TB --- src/utils/utils.go | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/utils/utils.go b/src/utils/utils.go index 2acdb7d..6c9ae36 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -7,27 +7,41 @@ import ( ui "github.com/cjbassi/termui" ) +var ( + KB = uint64(math.Pow(2, 10)) + MB = uint64(math.Pow(2, 20)) + GB = uint64(math.Pow(2, 30)) + TB = uint64(math.Pow(2, 40)) +) + func BytesToKB(b uint64) float64 { - return float64(b) / math.Pow10(3) + return float64(b) / float64(KB) } func BytesToMB(b uint64) float64 { - return float64(b) / math.Pow10(6) + return float64(b) / float64(MB) } func BytesToGB(b uint64) float64 { - return float64(b) / math.Pow10(9) + return float64(b) / float64(GB) +} + +func BytesToTB(b uint64) float64 { + return float64(b) / float64(TB) } func ConvertBytes(b uint64) (float64, string) { - if b >= 1000000000 { - return BytesToGB(uint64(b)), "GB" - } else if b >= 1000000 { - return BytesToMB(uint64(b)), "MB" - } else if b >= 1000 { - return BytesToKB(uint64(b)), "KB" - } else { + switch { + case b < KB: return float64(b), "B" + case b < MB: + return BytesToKB(b), "KB" + case b < GB: + return BytesToMB(b), "MB" + case b < TB: + return BytesToGB(b), "GB" + default: + return BytesToTB(b), "TB" } }