Files
gopeed/pkg/util/bytefmt.go
2021-03-25 19:38:23 +08:00

24 lines
448 B
Go

package util
import (
"fmt"
"math"
)
var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"}
func ByteFmt(size int64) string {
if size == 0 {
return "unknown"
}
fs := float64(size)
p := int(math.Log(fs) / math.Log(1024))
val := fs / math.Pow(1024, float64(p))
_, frac := math.Modf(val)
if frac > 0 {
return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p])
} else {
return fmt.Sprintf("%d%s", int(val), unitArr[p])
}
}