add bytecountdecimal

This commit is contained in:
Zack Scholl 2019-04-29 17:16:38 -06:00
parent 005a206460
commit 3685a887b8
1 changed files with 13 additions and 0 deletions

View File

@ -96,3 +96,16 @@ func GetRandomName() string {
result = mnemonicode.EncodeWordList(result, bs)
return strings.Join(result, "-")
}
func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}