diff options
| author | bingoohuang <bingoo.huang@gmail.com> | 2020-05-29 10:00:07 +0800 |
|---|---|---|
| committer | bingoohuang <bingoo.huang@gmail.com> | 2020-05-29 10:00:07 +0800 |
| commit | accb4964b7a880b0cb68fcf1262426d3c7793403 (patch) | |
| tree | 15d2e1f35163599bdc6c747aa9e2492464265010 /weed/util/bytes.go | |
| parent | 21d0a013d5912c62af43e418b613ae237f68e003 (diff) | |
| download | seaweedfs-accb4964b7a880b0cb68fcf1262426d3c7793403.tar.xz seaweedfs-accb4964b7a880b0cb68fcf1262426d3c7793403.zip | |
util.BytesToHumanReadable
Diffstat (limited to 'weed/util/bytes.go')
| -rw-r--r-- | weed/util/bytes.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/weed/util/bytes.go b/weed/util/bytes.go index d72d199f8..0650919c0 100644 --- a/weed/util/bytes.go +++ b/weed/util/bytes.go @@ -6,6 +6,22 @@ import ( "io" ) +// BytesToHumanReadable returns the converted human readable representation of the bytes. +func BytesToHumanReadable(b uint64) string { + const unit = 1024 + if b < unit { + return fmt.Sprintf("%d B", b) + } + + div, exp := uint64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + + return fmt.Sprintf("%.2f %ciB", float64(b)/float64(div), "KMGTPE"[exp]) +} + // big endian func BytesToUint64(b []byte) (v uint64) { |
