分割时浮动不返回小数

时间:2016-09-17 04:00:05

标签: android

我正在划分2个数字,并希望在适当时返回小数。但它正在返回所有内容的整数。

float temp = (twoMileInSeconds - 774) / 6;
timeIndex = (int)Math.floor(temp);

debug1.setText("Before rounding: " + temp);
debug2.setText("After rounding: " + timeindex);

由于

2 个答案:

答案 0 :(得分:1)

什么数据类型' twomileinsecods' ?如果它是int,那么这就是问题......或者如果你把6.0而不是6。

要确保在等号右侧发生的除法导致浮点运算,请确保右侧的所有数据类型都已浮动。问题在于,在分裂已经发生之后它会浮动。

答案 1 :(得分:1)

如果你将类型转换为浮动,那么它也可以完美地工作,因为你已经采用了变量twoMileInSeconds,它也可以是整数,所以你也可以采用这种方法。

float temp = (float) (twoMileInSeconds - 774) / 6;
        int timeIndex = (int)Math.floor(temp);
相关问题