From c2795104e512674d088c18e74d242be204b923f1 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 11 Apr 2019 12:57:32 -0700 Subject: [PATCH] add bytes --- src/utils/bytes.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/utils/bytes.go diff --git a/src/utils/bytes.go b/src/utils/bytes.go new file mode 100644 index 00000000..0341aaf0 --- /dev/null +++ b/src/utils/bytes.go @@ -0,0 +1,16 @@ +package utils + +import "fmt" + +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]) +}