The Math.round
method returns a long
(or an int
if you pass in a float
), and Java's integer division is the culprit. Cast it back to a double
, or use a double
literal when dividing by 10
. Either:
double total = (double) Math.round((num / sum * 100) * 10) / 10;
or
double total = Math.round((num / sum * 100) * 10) / 10.0;
Then you should get
27.3