th3ramr0d
th3ramr0d

Reputation: 484

Float not returning decimal when dividing

I am dividing 2 numbers and want to return a decimal when appropriate. But it is returning whole number on everything.

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

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

Thanks

Upvotes: 1

Views: 433

Answers (2)

Preetika Kaur
Preetika Kaur

Reputation: 2031

If you will type cast to float then also it will work perfectly as you have taken a variable twoMileInSeconds and it can be integer as well so you can go for this approach as well.

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

Upvotes: 1

Nerdy Bunz
Nerdy Bunz

Reputation: 7497

What data type is 'twomileinsecods' ? If its int, then that is the problem... or maybe if you put 6.0 instead of 6.

To ensure that the division taking place to the right of the equals sign results in a float, make sure all data types on the right are already floats. The problem is that it's casting to float AFTER the division has already taken place.

Upvotes: 1

Related Questions