gpt4 book ai didi

Golang 如何将一个整数和一个 float 相乘

转载 作者:IT王子 更新时间:2023-10-29 01:16:42 31 4
gpt4 key购买 nike

我正在尝试调整图像的尺寸,但在编译时遇到constant 0.8 truncated to integer 错误。这是我的代码

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)

我是 golang 的新手,但是这里的代码给了我错误

    height := int(heightImg * a) 
width := int(widthImg * a)

任何建议都很好

最佳答案

如果你想乘 float ,你需要把数字转换成 float :

height := int(float64(heightImg) * a) 
width := int(float64(widthImg) * a)

关于Golang 如何将一个整数和一个 float 相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768377/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com