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
}
posted on 2020-02-18 16:54  新手老猿  阅读(5249)  评论(0编辑  收藏  举报