Created
February 23, 2022 16:24
-
-
Save lzambarda/832caf0222f346e6eb3efc0afb391401 to your computer and use it in GitHub Desktop.
Humanize size SI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can be easily modified to work with binary with a unitStep of 1024 and iB as a suffix instead of B. | |
func HumanizeSize(b int64) string { | |
const unitStep = 1000 | |
const units = "BkMGTPE" | |
v := float64(b) | |
exp := 0 | |
for v > unitStep { | |
v /= unitStep | |
exp++ | |
} | |
return fmt.Sprintf("%.1f %cB", v, units[exp]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment