Reputation: 10039
I have a problem with my division on android:
double test= 100/ 1280;
double test2 = 0.2354;
System.out.println(test);
System.out.println(test2);
I have
0.0
0.2354
I'd like to have
0.078125
0.2345
Thanks
Upvotes: 4
Views: 6876
Reputation: 380
if you specify one of the numbers in your division with a decimal, i.e.
double test = 100.0/1280;
It will give you the desired output.
The reason you are not getting the correct result, is that when dividing two ints, the resulting type will also be an int. In order to avoid this, you have to typecast one of the operands in the division to a double, or - as a shorter solution when you are explicitly setting the number in the operation and not using variables - you can add the ".0" to one of the numbers.
Upvotes: 5