init: 1.0.0
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user