35 lines
649 B
Go
35 lines
649 B
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func FloatRoundString(in float64, place int) string {
|
|
|
|
return Float64ToString(FloatRound(in, place))
|
|
}
|
|
|
|
func FloatRound(in float64, place int) float64 {
|
|
// 限制 place 范围在合理区间
|
|
if place < 0 {
|
|
place = 0
|
|
}
|
|
|
|
// 使用 strconv.FormatFloat 直接格式化,避免多次条件判断
|
|
str := strconv.FormatFloat(in, 'f', place, 64)
|
|
num, _ := strconv.ParseFloat(str, 64)
|
|
return num
|
|
}
|
|
|
|
func RateToFloat64(s string) float64 {
|
|
// 去掉百分号
|
|
s = strings.TrimSuffix(s, "%")
|
|
// 转换为 float64
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return f / 100
|
|
}
|