Convert an integer to a float number

Issue

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?

Solution

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)
}

Answered By – zmb

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.