17 lines
365 B
Go
17 lines
365 B
Go
package backup
|
|
|
|
import "fmt"
|
|
|
|
// FormatSize formats a byte count as a human-readable string.
|
|
func FormatSize(n int64) string {
|
|
const unit = 1024
|
|
if n < unit {
|
|
return fmt.Sprintf("%d B", n)
|
|
}
|
|
div, exp := int64(unit), 0
|
|
for b := n / unit; b >= unit; b /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.1f %cB", float64(n)/float64(div), "KMGTPE"[exp])
|
|
} |