Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Fix byte conversions; add TB
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Aug 9, 2018
1 parent eb17f69 commit 77f728f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down

0 comments on commit 77f728f

Please sign in to comment.