mirror of
https://github.com/luckjiawei/podux.git
synced 2026-11-30 00:56:47 +00:00
19 lines
430 B
Go
19 lines
430 B
Go
package utils
|
|
|
|
import "fmt"
|
|
|
|
// FormatBytes formats bytes to human readable format
|
|
func FormatBytes(bytes uint64) string {
|
|
const unit = 1024
|
|
if bytes < unit {
|
|
return fmt.Sprintf("%d B", bytes)
|
|
}
|
|
div, exp := uint64(unit), 0
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
units := []string{"KB", "MB", "GB", "TB", "PB", "EB"}
|
|
return fmt.Sprintf("%.2f %s", float64(bytes)/float64(div), units[exp])
|
|
}
|