Fix byte conversions; add TB

This commit is contained in:
Caleb Bassi 2018-08-08 22:30:24 -07:00
parent eb17f69606
commit 77f728fb44

View File

@ -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"
}
}