Answers for "golang format float to 1 decimal places"

Go
0

golang truncate float to 1 decimal place

x := 12.3456
fmt.Println(math.Floor(x*100)/100) // 12.34 (round down)
fmt.Println(math.Round(x*100)/100) // 12.35 (round to nearest)
fmt.Println(math.Ceil(x*100)/100)  // 12.35 (round up)
Posted by: Guest on January-04-2022
1

convert float to int golang

package main
import "fmt"
func main() {
  var x float64 = 5.7
  var y int = int(x)
  fmt.Println(y)  // outputs "5"
}
Posted by: Guest on August-11-2021

Code answers related to "golang format float to 1 decimal places"

Browse Popular Code Answers by Language