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;
}
Answer from jpdymond on Stack Overflow
🌐
Quora
quora.com › How-can-I-round-a-number-to-1-decimal-digit-in-Java
How to round a number to 1 decimal digit in Java - Quora
Answer (1 of 12): While many of the answers are about String truncation, The question is about rounding (not truncation). Below is how to round a double value to a 1 decimal digit in Java: [code]public class RoundDouble { public static void main(String[] args) { double x = 1.5569834; do...
🌐
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
November 9, 2020 - 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 - In this quick tutorial, we’ll learn how to round a number to n decimal places in Java. 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. For that, and also for rounding, we can use the BigDecimal class. Let’s start with the core—Math.round()—this is typically the way to go.
🌐
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'?

🌐
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
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(); } }
🌐
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 - Below are four methods to round decimals in Java, each with its own limitations. 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. Using the first 50 decimal digits of pi, the result is 3. Method 1: Rounded using Math.round() Input value: 3.14159265358979323846264338327950288419716939937510 -------- Rounded using Math.round(): 3
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Round to One Decimal Place in Java | Study.com
This rounds the input 'value' to one decimal place. Math Functions - Java includes a special math function called 'round' that will perform the rounding for us.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 776097 › java › Rounding-decimals
Rounding decimals (Java in General forum at Coderanch)
Since you posted in a Java forum let's assume you have the number in a Java double variable. If you want to round it to N digits you do this: 1. Multiply it by 10 to the power N 2. Add 0.5 3. Divide it by 10 to the power N Or you could follow this tutorial which I just found on line: How to Round a Number to N Decimal Places in Java.
🌐
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
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.

🌐
GameDev.net
gamedev.net › forums › topic › 508767-rounding-to-1-decimal-place-in-java › 508767
rounding to 1 decimal place in Java - For Beginners - GameDev.net
June 11, 2008 - You may find solutions that seem to work fine, but in reality, you CANNOT round a double to 1 decimal place, because a double is represented as a binary fraction, not a decimal fraction. There are no decimal places in a double. Most finite decimal fractions like 0.1 CANNOT be represented exactly ...
🌐
Programiz
programiz.com › java-programming › examples › round-number-decimal
Java Program to Round a Number to n Decimal Places
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. The 4 decimal places are given by the format .4f. This means, print only up to 4 places after the dot (decimal places), and f means to print the floating-point number. import java.math.RoundingMode; import java.text.DecimalFormat; public class Decimal { public static void main(String[] args) { double num = 1.34567; DecimalFormat df = new DecimalFormat("#.###"); df.setRoundingMode(RoundingMode.CEILING); System.out.println(df.format(num)); } }
🌐
Educative
educative.io › answers › how-to-use-the-java-mathround-method
How to use the Java Math.round() method
import java.lang.Math; // Needed ... decimal places, in this case 2 decimal places we multiply it by 100.0 , pass it to the Math.round() method and then divide it by 100.0. Note: To round a number to a specific decimal place, ...
🌐
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.
🌐
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 ...
🌐
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
Our round() method will take a decimal value and the number of decimal places to round as parameters and will return the rounded number. The code for this is shown below. However, it is recommended to use the below code as it can give unexpected ...