0
Average =10/4; Returns 2.0 not 2.5 Pls suggest me to correct this
I'm a Java beginner
1 Answer
+ 1
In Java, an integer divided by an integer always returns an integer.
So if 999 is divided by 1000, the answer instead of being 0.999, will be 0, because the decimal part is cut from the answer. In your case, 10 / 4 = 2.5. The decimal part 0.5 is cut out and the answer is 2.
What you need to do is divide by a float or double
10.0 / 4 or 10 / 4.0 or 10.0 / 4.0
If it is a variable you can type cast it by doing
float var = (float) variable_name / another_var;