If you divide two integers
result is always integer
You have to cast one number from int
to double
.
double test = (double)29/6;
System.out.printf("%.2f",test);
System.out.println(test);
Note that if you just write number, it is considered int
There is important thing, following code prints 4.00 too :
double test = (double)(29/6);
System.out.printf("%.2f",test);
System.out.println(test);
Because first it divides two integers, therefore the result is also integer and then it is casted to double.
Using (double)29/6
is same as ((double)29)/6
, because casting has higher priority.