separated gb calculation from BytesCount (#593)

* separated gb calculation from BytesCount

* ignoring function in lint
This commit is contained in:
Enrique Jose Avila Asapche 2022-08-19 07:27:19 +03:00 committed by GitHub
parent 30d30ce96f
commit 4c744ae88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,13 +23,23 @@ func ByteCount(b uint64) string {
if b < unit {
return fmt.Sprintf("%dB", b)
}
bGb, exp := MBToGB(b)
return fmt.Sprintf("%.1f%cB", bGb, "KMGTPE"[exp])
}
func MBToGB(b uint64) (float64, int) {
const unit = 1024
if b < unit {
return float64(b), 0
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB",
float64(b)/float64(div), "KMGTPE"[exp])
return float64(b) / float64(div), exp
}
func Copy(b []byte) []byte {