Division Double Android
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
Solution 1:
Try this
double test= (double) 100/ 1280;
Solution 2:
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.
Post a Comment for "Division Double Android"