我刚开始在Go中发现它没有“toFixed”函数(如在JavaScript中),它可以实现你想要的,甚至也不是“圆形”函数。 我从其他地方获取了一个单行轮函数,并且还使用了取决于round()的固定():

func round(num float64) int {
    return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
    output := math.Pow(10, float64(precision))
    return float64(round(num * output)) / output
}
用法:
fmt.Println(toFixed(1.2345678, 0))  // 1
fmt.Println(toFixed(1.2345678, 1))  // 1.2
fmt.Println(toFixed(1.2345678, 2))  // 1.23
fmt.Println(toFixed(1.2345678, 3))  // 1.235 (rounded up)