45 lines
810 B
Go
45 lines
810 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/dustin/go-humanize"
|
|
"math"
|
|
"math/big"
|
|
)
|
|
|
|
type flowCalculation struct{}
|
|
|
|
func FlowCalculation() flowCalculation {
|
|
return flowCalculation{}
|
|
}
|
|
|
|
// Parse
|
|
// @description: 解析流量,序列化为字符串
|
|
// @receiver flowCalculation
|
|
// @param b
|
|
// @return string
|
|
func (flowCalculation) Parse(b int64) string {
|
|
b2 := big.Int{}
|
|
return humanize.BigBytes(b2.SetInt64(b))
|
|
}
|
|
|
|
// PrettyByteSizeGB
|
|
// @description:
|
|
// @param b
|
|
// @return string
|
|
func PrettyByteSizeGB(b int) string {
|
|
bf := float64(b)
|
|
ks := []string{"", "Ki", "Mi", "Gi"}
|
|
for i, unit := range ks {
|
|
if math.Abs(bf) < 1024.0 {
|
|
return fmt.Sprintf("%3.1f%sB", bf, unit)
|
|
}
|
|
if i+1 >= len(ks) {
|
|
return fmt.Sprintf("%3.1f%sB", bf, unit)
|
|
}
|
|
bf /= 1024.0
|
|
}
|
|
|
|
return fmt.Sprintf("%.1fYiB", bf)
|
|
}
|