I have these two variables
double num = 540.512 double sum = 1978.8
Then I did this expression
double total = Math.round((num/ sum * 100) * 10) / 10;
but I end up with 27.0.
In fact I have many other variables and when I use them in the expression I always get a 0 in the tenth's place.
When you round to the first decimal place, or to the nearest tenth, the number in the hundredth point place will determine whether you round up or down. If the hundredths digit is a number between 5 and 9, round up to the nearest whole number.
Here's the general rule for rounding: If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down.
Correct to the nearest 0.1k means you are required to round the answer up or down to the nearest 10th (So 0.11246 would be 0.1, and 0.2704 would be 0.2).
Helpful method I created a while ago...
private static double round (double value, int precision) { int scale = (int) Math.pow(10, precision); return (double) Math.round(value * scale) / scale; }
try this
for example
DecimalFormat df = new DecimalFormat("#.##"); df.format(55.544545);
output:
55.54
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With