How to convert an integer (int) to a floating point number (float64) in Go?
Hello everyone! I'm just starting to learn Go after Python and encountered a problem with type conversion. In Python, I'm used to floating point numbers and integers mixing easily in expressions, but in Go everything is strict.
I have a simple arithmetic mean calculator. I get a sum of integers (int) and a count of elements (also int). I need to calculate the average, which obviously can be fractional. But the compiler complains when I try to divide int by int and get a float.
Here's my code:
package main
import "fmt"
func main() {
sum := 10 // int
count := 3 // int
avg := sum / count // Want to get 3.333..., but get 3
fmt.Println(avg)
}
I understand that in Go, integer division d
No answers yet.
Log in to leave an answer