I have these two variables
double num = 540.512
double sum = 1978.8
Then I did this expression
double total = Math.round((
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