数学表达总是零吗?

时间:2014-06-14 10:53:39

标签: java

我不知道为什么下面的数学运算给了我0.0

Mathematical_expression:

double percentageAmongAll = ((itemClickedID+1)/(parent.getCount()));

Logcat_Output:

06-14 13:46:53.176: I/MySavedLocation(19361): parent.getCount() = 3
06-14 13:46:53.176: I/MySavedLocation(19361): itemClickedID+1 = 2
06-14 13:46:53.176: I/MySavedLocation(19361): percentageAmongAll = 0.0

1 个答案:

答案 0 :(得分:4)

itemClickedID+1parent.getCount()是整数表达式。整数除法总是返回整数,因此2/3返回0,然后转换为0.0

double percentageAmongAll =  ((double) itemClickedID+1)/(parent.getCount());

这会将itemClickedId转换为double,因此将执行双重除法。