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.

Answer from curtisk on Stack Overflow
Top answer
1 of 16
897

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.

2 of 16
537

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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-round-a-number-to-n-decimal-places
Java Program to Round a Number to n Decimal Places - GeeksforGeeks
January 22, 2026 - In this approach, we first Multiply the number by 10n using the pow() function of the Math class. Then the number is rounded to the nearest integer. At last, we divide the number by 10n.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ how to round a number to n decimal places in java
How to Round a Number to N Decimal Places in Java | Baeldung
September 24, 2025 - Java provides two primitive types that we can use for storing decimal numbers: float and double. Double is the default type: ... However, we should never use either type for precise values like currencies.
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
The precision of a double in Java is 10-324 decimal places, although true mathematical precision can suffer due to issues with binary arithmetic.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ round-number-decimal
Java Program to Round a Number to n Decimal Places
Try Programiz PRO! ... public class Decimal { public static void main(String[] args) { double num = 1.34567; System.out.format("%.4f", num); } } ... In the above program, we've used the format() method to print the given floating-point number num to 4 decimal places.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-set-decimal-places-in-Java
How to set decimal places in Java - Quora
My suggestion is to multiply your floating-point number by 10, 100, or 1000 to get the decimal places you wish, and use the round() function on the multiplied number. Then, take the integer result, convert it to float, and then divide the float ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
1 week ago - Weโ€™ll get a String back from these methods but can always convert the result back to a double if needed. The String.format() method takes two arguments. Firstly, the format we want to apply, and secondly, the arguments referenced by the format. To truncate to two decimal places, weโ€™ll use the format String โ€œ%.2fโ€:
Find elsewhere
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - We can use DecimalFormat("0.00") to ensure the number is round to 2 decimal places. ... package com.mkyong.math.rounding; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalExample { private static final DecimalFormat ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
January 15, 2025 - The result is a string with the value rounded to 2 decimal places. ... The String.format() method is easy and useful when you need to display formatted numbers, but like DecimalFormat, the result is a string and not suitable for further numerical ...
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Format-double-Java-printf-example
How to format a Java double with printf example
It is a common requirement to format currencies to two decimal places. You can easily achieve this with the Java printf function. Just use %.2f as the format specifier.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ how do i round of to a specific decimal point?
r/java on Reddit: How do I round of to a specific decimal point?
February 16, 2014 -

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'?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-set-precision-for-double-values-in-java
How to Set Precision For Double Values in Java? - GeeksforGeeks
July 12, 2025 - Here we are given a double value, the task is to set its precision value to specific decimal places. It is illustrated below illustrations as follows: Input : val = 1 Output : 1.0000 Upto 4 decimal places
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - In Java, we have two primitive types that represent decimal numbers, float and decimal: double myDouble = 7.8723d; float myFloat = 7.8723f; The number of decimal places can be different depending on the operations being performed. In most cases, weโ€™re only interested in the first couple of decimal places.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-format-decimal-places-in-java-435617
How to format decimal places in Java | LabEx
Learn efficient techniques to format decimal places in Java, covering DecimalFormat, String.format(), and NumberFormat for precise number representation in various programming scenarios.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 753436 โ€บ java โ€บ count-digits-decimal-point
How to count digits after decimal point (Beginning Java forum at Coderanch)
August 6, 2022 - That means, if you subtract exactly 12.3456 from it, do you get exactly 0? If so, define them as Strings, create a BigDecimal (or consider this method). BigDecimal is (sort of) represented in decimal, and has methods to give you the number of places after the decimal point.
๐ŸŒ
Mathbits
mathbits.com โ€บ JavaBitsNotebook โ€บ DataBasics โ€บ Money.html
Controlling Decimal Output - JavaBitsNotebook.com
Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms of Use | JavaMathBits.com Controlling Decimal Output ยท It is often necessary to print values with a designated number of decimal places. For example, when printing amounts of money, it is customary to have two decimal places ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 681508 โ€บ java โ€บ maximum-decimal-places-float-double
The maximum decimal places for a float and a double: Where are they exactly defined? (Beginning Java forum at Coderanch)
June 27, 2017 - So I am not an expert, but basically, ... base for the exponent is "10", we don't need to store that. we can just store the "3". now, if we know we have 10 places/bytes/digits to use to store this number, we can decide the first 6 are for the "number" part, and the last four are ...
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ business courses โ€บ business 104: information systems and computer applications
How to Round to 2 Decimal Places in Java - Lesson | Study.com
January 5, 2018 - By tweaking the input parameter slightly, we can use some creative math to give us the two decimal places. Because the Math.round function rounds to the nearest whole number, we will first multiply the base * rate by 100.0. The .0 tells Java we intend to deal with a float or double value.