且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的浮动变量语句不断给我一个'不能隐式转换类型双浮动'错误?

更新时间:2023-11-14 13:40:40

问题是,当你执行乘法(或其他算术运算)你正在使用一个文字,如0.15。默认情况下,这个值将被编译器视为一个双精度值,所以当乘上一个浮点数时,会导致较大的双精度结果。为了解决这个问题,你需要将文字标记为.15f,以便将它们视为浮点文字。

The problem is that when you perform your multiplication (or other arithmetic operation) you are working with a literal such as 0.15. This value by default will be treated as a double by the compiler and so when multiplied against a float it results in the larger double result. To get around this you need to mark the literals as .15f so that they are treated as float literals.