golang中float64 保留小数点后指定的位数

erlang/golang 码农 / 2023-08-08 / 原文

代码如下:

// Decimal float64 保留小数点后位数
// value float64 浮点数
// prec int 需保留小数点后的位数
func Decimal(value float64,prec int) float64 {
	value, _ = strconv.ParseFloat(strconv.FormatFloat(value,'f',prec,64), 64)
	return value
}