0

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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
im_mangesh
  • 177
  • 1
  • 1
  • 13

1 Answers1

2

(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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483