I have this code:
float result = (85000/12)*5;
System.out.println(result );
the value of result
is coming 35415.0 but the actual calculation result
is 35416.66 .
I tried all data types but didn't get actual value. how should i get it.
I have this code:
float result = (85000/12)*5;
System.out.println(result );
the value of result
is coming 35415.0 but the actual calculation result
is 35416.66 .
I tried all data types but didn't get actual value. how should i get it.
(85000 / 12) * 5
; is evaluated as you see it: 85000 / 12
is evaluated in integer arithmetic - the remainder is discarded. The result of that is multiplied by 5
.
To retain the float
type of the compile time evaluable constant expression, use 85000f / 12 * 5
.
I always promote the first coefficient, (i) for clarity, (ii) because something like 85000 / 12 * 5f
will exhibit truncation effects.