Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Help with "Type Conversion" in Golang

Hi everyone, hope you are all well...

I am struggling with this problem in Golang "Type Conversion".

Here is my code:*


package main

import "fmt"

func main() {
var length float64 = 3.0
var width int = 4
var area int

area = int(length * float64(width) + 0.5) // Type conversion from float64 to int with rounding

fmt.Println("Area is:", area)
}


*the output is 12 nut the code says: Please check if the code for finding area is correct. *

*is there anything wrong with my code or this is a bug??
by

1 Answer

sam5epi0l
Hi, We need convert type of length to integer only.


area = int(length)*width

Login / Signup to Answer the Question.