Golang截取小数位数

180it 2020-10-24 PM 2959℃ 0条

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
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

Golang截取小数位数