How to round a number to n decimal places in Java

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: gives the output: 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 … Read more