Thursday, September 27, 2018

go - Convert an integer to a float number in Golang



How do I convert an integer value to float64 type?




I tried



float(integer_value)


But this does not work. And can't find any package that does this on Golang.org



How do I get float64 values from integer values?


Answer



There is no float type. Looks like you want float64. You could also use float32 if you only need a single-precision floating point value.




package main

import "fmt"

func main() {
i := 5
f := float64(i)
fmt.Printf("f is %f\n", f)
}


No comments:

Post a Comment