Explain the following Math functions in Java: Math.round()
How to round a number to n decimal places in Java - Stack Overflow
Math round java - Stack Overflow
How do I round of to a specific decimal point?
Videos
Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.
Example:
DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
Double d = n.doubleValue();
System.out.println(df.format(d));
}
gives the output:
12
123.1235
0.23
0.1
2341234.2125
EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:
Double d = n.doubleValue() + 1e-6;
To round down, subtract the accuracy.
Assuming value is a double, you can do:
(double)Math.round(value * 100000d) / 100000d
That's for 5 digits precision. The number of zeros indicate the number of decimals.
You could do something like this:
CopyDouble.valueOf(new DecimalFormat("#.##").format(
centimeters))); // 2 decimal-places
If you really wanted Math.round:
Copy(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
You can have 3 decimal places by using 1000, 4 by using 10000 etc. I personally like the first option more.
In order to use the Math.round method you need to change only one line in your code:
Copydouble inches = Math.round(centimeters / 2.54);
If you want to keep 2 decimal digits you can use this:
Copydouble inches = Math.round( (centimeters / 2.54) * 100.0 ) / 100.0;
By the way I suggest you a better way to deal with these problems without rounding.
Your problem is only about displaying, so you don't need to change the model of the data, you can just change its display. To print the numbers in the format you need, you can let all your logic code like this and print the result in the following way:
Add this import at the beginning of your code:
Copyimport java.text.DecimalFormat;Print the output in this way:
CopyDecimalFormat df = new DecimalFormat("#.##"); System.out.println(df.format(inches) + " Inch Is " + df.format(centimeters) + " centimeters");
The string "#.##" is the way your number will be displayed (in this example with 2 decimal digits).
For example, this:
public static void main(String[] args) {
double x = 5.56;
double y = 7.863;
double result = x * y;
System.out.println(result);
}
}prints a result of '43.71828'. How would I round the output off to '43.72'?