Golang截取小数位数
package main
import (
"fmt"
"strconv"
)
func main() {
var ff float64
ff = -1.355123156
ff = FloatRound(ff, 4)
fmt.Println(ff) // 输出 -1.3551
}
// 截取小数位数
func FloatRound(f float64, n int) float64 {
format := "%." + strconv.Itoa(n) + "f"
res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
return res
}