go-utils/utils/bytes.go
2021-08-19 15:17:58 +00:00

24 lines
383 B
Go

package utils
import "fmt"
/*
Format bytes e.g.
Bytes(53) // out: "53 B"
Bytes(1522) // out: "1.5 kB"
*/
func Bytes(b uint64) 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])
}