mirror of
https://github.com/wonli/aqi.git
synced 2025-12-24 10:40:58 +08:00
19 lines
329 B
Go
19 lines
329 B
Go
package format
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
func Bites(size float64) string {
|
|
unit := []string{"B", "KB", "MB", "GB", "TB", "PB"}
|
|
s := math.Floor(math.Log(size) / math.Log(1024))
|
|
i := int(s)
|
|
|
|
if i < len(unit) {
|
|
return fmt.Sprintf("%.2f %s", size/math.Pow(1024, s), unit[i])
|
|
}
|
|
|
|
return fmt.Sprintf("%f %s", size, unit[0])
|
|
}
|