float62 32 格式化为两位
This commit is contained in:
@@ -621,3 +621,16 @@ func UniqueStr(arr []string) []string {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Float 泛型约束,只允许 float32 / float64
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
// RoundFloat 四舍五入保留n位小数,泛型版本
|
||||
func RoundFloat[T Float](val T) T {
|
||||
shift := math.Pow10(NumberTwo)
|
||||
f := float64(val)
|
||||
res := math.Round(f*shift) / shift
|
||||
return T(res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user