blob: ce7c4fad60773da5adf6928b2541922701faaf4a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package format
import "fmt"
func FormatFileSize(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
|