Skip to content

Instantly share code, notes, and snippets.

@lzambarda
Created February 23, 2022 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzambarda/832caf0222f346e6eb3efc0afb391401 to your computer and use it in GitHub Desktop.
Save lzambarda/832caf0222f346e6eb3efc0afb391401 to your computer and use it in GitHub Desktop.
Humanize size SI
// 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