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.

🌐
Programiz
programiz.com › java-programming › examples › round-number-decimal
Java Program to Round a Number to n Decimal Places
import java.math.RoundingMode; ... } } ... In the above program, we've used DecimalFormat class to round a given number num. We declare the format using the # patterns #.###. This means we want num up to 3 decimal places....
Discussions

How do I round of to a specific decimal point?
If you just need to print rounded, try format method: System.out.format("%.2f%n",result); More on reddit.com
🌐 r/java
6
0
February 16, 2014
I want to round a decimal number, for example 2100.000000, to two digits after the decimal dot, even if it's two 0s.
You will not be able to format the value inside a double variable. You can round it, but you cant cut off additional 0's at the end. 2100.000000 is the same as 2100.00 from a numeric perspective. You can however format the double into a String as you already did yourself with the formatted variable. Either use the DecimalFormat or String.format. More on reddit.com
🌐 r/javahelp
13
4
February 15, 2023
round up to 2 decimal places in java? - Stack Overflow
I have read a lot of stackoverflow questions but none seems to be working for me. i am using math.round() to round off. More on stackoverflow.com
🌐 stackoverflow.com
java - Using Math.round to round to one decimal place? - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
March 13, 2025
🌐
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 - Syntax: number = Math.round(number*Math.pow(10,n))/Math.pow(10,n); Java · class GFG { public static void main(String[] args) { double number = 12.983665; int n = 3; number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n); ...
🌐
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 - DecimalFormat df = new DecimalFormat("###.###"); System.out.println(df.format(PI)); // OUTPUTS: Value with 3 digits after decimal point 3.142 · DecimalFormat allows us to explicitly set rounding behavior explicitly, giving more control of the output than the String.format() used above. To round doubles to n decimal places, we can write a helper method:
🌐
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'?

🌐
Java67
java67.com › 2020 › 04 › 4-examples-to-round-floating-point-numbers-in-java.html
4 Examples to Round Floating-Point Numbers in Java up to 2 Decimal Places | Java67
December 18, 2024 - DecimalFormat df = new DecimalFormat("#.00"); float number = Float.valueOf(df.format(decimal)); The #.00 is for rounding up to 2 decimal places, if you want to round up to 3 or 4 places, just use #.000 or #.0000 to create DecimalFormat.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 776097 › java › Rounding-decimals
Rounding decimals (Java in General forum at Coderanch)
August 17, 2023 - Using Sam's example of 87.3923 rounded to the nearest thousandth, that would be 1. 87.3923 * 1000 = 87392.3 2. 87392.3 + 0.5 = 87392.8 3. Truncated = 87392.0 4. 87392.0 / 1000 = 87.392 There are several possible gotcha's here for negative numbers and some special values of double. It's simpler to use Math.round() which handles those for you.
🌐
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 calculations. To make the rounding process reusable, you can create a custom utility method that encapsulates one of the approaches shown above. ... import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double value = 12.34567; double roundedValue = roundToNDecimalPlaces(value, 2); System.out.println("Rounded value: " + roundedValue); } public static double roundToNDecimalPlaces(double value, int decimalPlaces) { BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP); return bd.doubleValue(); } }
🌐
LabEx
labex.io › tutorials › java-rounding-floating-point-numbers-in-java-117452
Java Rounding Techniques | Programming Tutorials | LabEx
August 24, 2020 - After that, run it using java filename. We can use Math.round() to round decimal numbers. It can be used when we want to round a number to zero decimal places. public static void main(String args[]) { double pi = 3.14159265359; double oneThird ...
🌐
John Dalesandro
johndalesandro.com › blog › rounding-decimals-in-java-avoiding-round-off-errors-with-bigdecimal
Rounding Decimals in Java: Avoiding Round-Off Errors with BigDecimal
March 21, 2025 - As a general rule, use BigDecimal to get the most reliable and accurate results. We will use the first 50 decimal digits of pi for demonstration: 3.14159265358979323846264338327950288419716939937510 ... This method only rounds to the nearest integer, making it unsuitable for rounding to decimal places...
🌐
Scaler
scaler.com › home › topics › round off in java
Java Math.round() - Scaler Topics
April 19, 2024 - So, 1087.12345567 rounded to 4 decimal places is 1087.1234, and the last decimal place (4) is rounded up using the CEILING mode, resulting in 5. Hence, the output is 1087.1235. Note: We need to import the DecimalFormat and RoundingMode classes in Java using the import statement, as they are not part of the default java.lang package. Round-off is used to convert decimal values into integers. The java.lang.Math class has Math.ceil(), Math.floor(), and Math.round() methods to round off floating-point values.
🌐
YouTube
youtube.com › tanuv90
Java: Rounding Numbers (Math.round(), DecimalFormat & printf) - YouTube
GitHub repo with examples https://github.com/SleekPanther/java-math-improved-round Java enables you to do almost anything, especially tasks involving numbers...
Published   April 13, 2013
Views   80K
🌐
Quora
quora.com › In-Java-how-do-I-round-a-number-or-variable-to-whatever-amount-of-decimal-places-I-want
In Java, how do I round a number or variable to whatever amount of decimal places I want? - Quora
Answer (1 of 7): Well this depends a bit. Do you mean that you want some text that represents the number to be to a number? Are you using a float-type (double and float in Java)? Are you using BigDecimal? Since you use the tag “Learning to Program” I’m going to assume that you, or people that mi...
🌐
How to do in Java
howtodoinjava.com › home › java basics › round off a float number to 2 decimals in java
Round Off a Float Number to 2 Decimals in Java
February 16, 2024 - Note that we can use the given solutions to round off to any number of places according to requirements. double number = 4.56789; // 1. Using Math.round() double rounded = Math.round(number * 100.0) / 100.0; // 4.57 // 2. Using BigDecimal BigDecimal ...
🌐
Mkyong
mkyong.com › home › java › java – how to round double / float value to 2 decimal places
Java – How to round double / float value to 2 decimal places - Mkyong.com
February 25, 2025 - We can use DecimalFormat("0.00") to ensure that a number always rounds to 2 decimal places. By default, DecimalFormat uses RoundingMode.HALF_EVEN, but we can change the rounding mode using setRoundingMode(RoundingMode) if needed.
🌐
Studytonight
studytonight.com › java-examples › how-to-round-a-number-to-n-decimal-places-in-java
How to Round a Number to N Decimal Places in Java? - Studytonight
2 weeks ago - Java's Math class provides us lots of methods for performing basic mathematical operations. Math.round() is used to round decimal numbers. It can be used when we just want to round a number to zero decimal places.