mirror of
https://code.rocketnine.space/tslocum/twins.git
synced 2024-11-27 14:28:15 +01:00
18 lines
307 B
Go
18 lines
307 B
Go
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func formatFileSize(b int64) 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("%.0f %cB",
|
||
|
float64(b)/float64(div), "KMGTPE"[exp])
|
||
|
}
|