双值返回0 [英] Double value returns 0
本文介绍了双值返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是一个例子:
Double d = (1/3);
System.out.println(d);
这将返回0,而不是0.33333 ......应该如此。
This returns 0, not 0.33333... as it should.
有谁知道吗?
推荐答案
那是因为 1 $ c $当你没有另外指定时,c>和
3
被视为整数
,所以 1 / 3
计算为整数
0
然后将其转换为 double
0
。要解决此问题,请尝试(1.0 / 3)
,或者 1D / 3
以明确说明您正在处理双值。
That's because 1
and 3
are treated as integers
when you don't specify otherwise, so 1/3
evaluates to the integer
0
which is then cast to the double
0
. To fix it, try (1.0/3)
, or maybe 1D/3
to explicitly state that you're dealing with double values.
这篇关于双值返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文