Golang how can i multiply an integer and float
I'm trying to resize an image, but when I compile, I get a constant of 0.8 truncated to an integer precision . This is my code
b := img.Bounds() heightImg := b.Max.Y // height of image in pixels widthImg := b.Max.X // width of image in pixels const a = .80 height := int(heightImg * a) // reduce height by 20% width := int(widthImg * a) // reduce width by 20% // resize image below which take in type int, int in the 2nd & 3rd parameter new_img := imaging.Resize(img,width,height, imaging.Lanczos)
I am new to golang but this code right here is giving me error
height := int(heightImg * a)
width := int(widthImg * a)
any suggestions would be great
+4
Rome torres
source
to share
2 answers
If you want to multiply floats, you need to convert the number to float:
height := int(float64(heightImg) * a) width := int(float64(widthImg) * a)
+12
JimB
source
to share
var xx float64 xx = 0.29 fmt.Println(xx, xx * 100)
Result - 28.9999999999999996, conversion to integer - 28
var xx float32 xx = 0.29 fmt.Println(xx * 100)
Result 29, convert to int 29
0
chen youfu
source
to share