How to convert Int data type to Float in Golang?

In Golang, the data types are bound to variables rather than the values, which means that, if you declare a variable as int, then you can store only integer type value in it, you cant assign character or string in it unless you convert the data type to required data type.

To convert an integer data type to float you can wrap the integer with float64() or float32.






// Golang program to convert Int data type to Float
package main
import (
func main() {
    // var declared x as int
    var x int64 = 5
    // y is a float64 type variable
    var y float64 = float64(x)
    // printing the values of x and y
    fmt.Printf("x = %d \n", x)
    fmt.Printf("y = %f \n", y)
    // to print a float upto a
    // specific number of decimal point
    fmt.Printf("\ny upto 3 decimal = %.3f", y)
    // getting the converted type
    fmt.Println("\n", reflect.TypeOf(y))



x = 5 
y = 5.000000 

y upto 3 decimal = 5.000

Explanation: Firstly we declare a variable x of type int64 with a value of 5. Then we wrap x with float64(), which converts the integer 5 to float value of 5.00. The %.3f formats the float value upto 3 decimal points.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.