Java 给定浮点值,从 double 到 float 的可能有损转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21645783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible lossy conversion from double to float, given float values?
提问by user3202989
if i'm not mistaken, "0.5" are decimal numbers; thus, making it a float value. but why is java telling me that it is a double?. the return statements are detected as errors by java, saying: "incompatible types:possible lossy conversion from double to float"
如果我没记错的话,“0.5”是十进制数;因此,使其成为浮点值。但为什么 java 告诉我它是一个双倍?返回语句被 java 检测为错误,说:“不兼容的类型:从双精度到浮点的可能有损转换”
public float typeDmgMultiplr(String type,String type2){
if(type.equalsIgnoreCase("grass")){
if(type2.equalsIgnoreCase("grass"))
return 0.5;
else if(type2.equalsIgnoreCase("poison"))
return 0.5;
else if(type2.equalsIgnoreCase("fire"))
return 0.5;
else
return 2.0;
}
else if(type.equalsIgnoreCase("fire")){
if(type2.equalsIgnoreCase("grass"))
return 2.0;
else if(type2.equalsIgnoreCase("poison"))
return 1.0;
else if(type2.equalsIgnoreCase("fire"))
return 0.5;
else
return 0.5;
}
else if(type.equalsIgnoreCase("water")){
if(type2.equalsIgnoreCase("grass"))
return 0.5;
else if(type2.equalsIgnoreCase("poison"))
return 1.0;
else if(type2.equalsIgnoreCase("fire"))
return 2.0;
else
return 0.5;
}
else{
if(type2.equalsIgnoreCase("grass"))
return 2.0;
else if(type2.equalsIgnoreCase("poison"))
return 0.5;
else if(type2.equalsIgnoreCase("fire"))
return 1.0;
else
return 1.0;
}
}
回答by pablosaraiva
You have two options.
你有两个选择。
One is change your method's return type to double.
一种是将方法的返回类型更改为 double。
The other is change the double values you are returning to float values, as many have said in the comments.
另一种是将您要返回的双精度值更改为浮点值,正如许多人在评论中所说的那样。
public float typeDmgMultiplr(String type,String type2){
if(type.equalsIgnoreCase("grass")){
if(type2.equalsIgnoreCase("grass"))
return 0.5f;
else if(type2.equalsIgnoreCase("poison"))
return 0.5f;
else if(type2.equalsIgnoreCase("fire"))
return 0.5f;
else
return 2.0f;
}
else if(type.equalsIgnoreCase("fire")){
if(type2.equalsIgnoreCase("grass"))
return 2.0f;
else if(type2.equalsIgnoreCase("poison"))
return 1.0f;
else if(type2.equalsIgnoreCase("fire"))
return 0.5f;
else
return 0.5f;
}
else if(type.equalsIgnoreCase("water")){
if(type2.equalsIgnoreCase("grass"))
return 0.5f;
else if(type2.equalsIgnoreCase("poison"))
return 1.0f;
else if(type2.equalsIgnoreCase("fire"))
return 2.0f;
else
return 0.5f;
}
else{
if(type2.equalsIgnoreCase("grass"))
return 2.0f;
else if(type2.equalsIgnoreCase("poison"))
return 0.5f;
else if(type2.equalsIgnoreCase("fire"))
return 1.0f;
else
return 1.0f;
}
}
回答by Stephen C
If I'm not mistaken,
0.5
are decimal numbers; thus, making it a float value.
如果我没记错的话,
0.5
是十进制数;因此,使其成为浮点值。
You should not rely solelyon your intuition when learning a new programming language.
你不应该依靠单纯学习新的编程语言时,你的直觉。
In fact, 0.5
is a double
literal. For a float
literal you need to write 0.5f
.
其实0.5
就是double
字面意思。对于float
文字,您需要编写0.5f
.
As The Java Language Specification (JLS 3.10.2) states:
正如 Java 语言规范 ( JLS 3.10.2) 所述:
A floating-point literal is of type
float
if it is suffixed with an ASCII letterF
orf
; otherwise its type isdouble
and it can optionally be suffixed with an ASCII letterD
ord
.
如果浮点文字
float
以 ASCII 字母F
或为后缀,则它属于类型f
。否则它的类型是double
,它可以选择后缀为 ASCII 字母D
或d
.
See also:
也可以看看:
回答by Ramesh Gupta
In java any decimal value without literal takes as Double
.
So you have to mention the Float (F)
behind the expression.
E.g. Float f = 10.5F;
在 java 中,任何没有文字的十进制值都作为Double
.
所以你必须提到Float (F)
后面的表达。例如Float f = 10.5F;
Hope this helps you. :)
希望这对你有帮助。:)
回答by Aiswarya S
In java, we have float and double as floating point literals, where doubleis the default data typeof floating point literals and not the float. This is the reason why java is telling you that 0.5 is a double.
在 java 中,我们有 float 和 double 作为浮点文字,其中double是浮点文字的默认数据类型,而不是 float。这就是 java 告诉您0.5 是 double 的原因。
Possible conversion:
可能的转换:
1) float a = (float) 0.5;
1) 浮动 a = (浮动) 0.5;
2) float a = 0.5f;
2) 浮动 a = 0.5f;
And reason for lossy conversion is because, double is bigger than float. When you try to fit-in the bigger one into the smaller one, you will get this error.
有损转换的原因是因为 double 比 float 大。当您尝试将较大的装入较小的时,您将收到此错误。