diff --git a/src/utils/utils.go b/src/utils/utils.go index 4b444be..64d066e 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -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]) +}