goext/lang/langext/bytes.go

17 lines
311 B
Go
Raw Normal View History

2022-10-27 16:00:57 +02:00
package langext
import "fmt"
func FormatBytesToSI(b uint64) string {
const unit = 1000
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("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}