How to convert Int data type to Float in Golang?


In Golang, it is easy to convert one data type to another using type conversion. In this tutorial, we will explore how to convert an integer to a float data type.

Converting Int to Float

In Golang, converting an integer to a float is a straightforward process. You can use the float64() function to convert an integer to a float data type.

Example

Here is an example −

package main

import "fmt"

func main() {
   i := 5
   f := float64(i)

   fmt.Println("The integer value is: ", i)
   fmt.Println("The float value is: ", f)
}

Output

The integer value is:  5
The float value is:  5

In the above example, we have an integer value of 5. We are using the float64() function to convert this integer value to a float data type. The float value is then assigned to a variable 'f'.

Conclusion

In conclusion, we have learned how to convert an integer to a float data type in Golang. The process is very straightforward, and all you need is to use the float64() function to perform the conversion.

Updated on: 05-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements