hi-server/pkg/tool/fromatFloat.go
2025-10-10 07:13:36 -07:00

27 lines
513 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tool
import (
"math"
"strconv"
)
func FormatFloat(num float64, decimal int) string {
// 默认乘1
d := float64(1)
if decimal > 0 {
// 10的N次方
d = math.Pow10(decimal)
}
// math.trunc作用就是返回浮点数的整数部分
// 再除回去小数点后无效的0也就不存在了
return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
}
func FormatStringToFloat(str string) float64 {
value, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return value
}