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
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM