Let's get started with a Microservice Architecture with Spring Cloud:
How to Round a Number to N Decimal Places in Java
Last updated: September 24, 2025
1. Overview
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:
double PI = 3.1415;
However, we should never use either type for precise values like currencies. For that, and also for rounding, we can use the BigDecimal class.
2. The Math.round() Method
Let’s start with the core—Math.round()—this is typically the way to go.
We can control n number of decimal places by multiplying and dividing by 10^n:
public static double roundAvoid(double value, int places) {
double scale = Math.pow(10, places);
return Math.round(value * scale) / scale;
}
However, it’s important to understand that this will truncate the value – which can result in an incorrect operation:
System.out.println(roundAvoid(1000.0d, 17));
// OUTPUTS: 92.23372036854776 !!
System.out.println(roundAvoid(260.775d, 2));
// OUTPUTS: 260.77 instead of expected 260.78
As a result, this method is listed here for learning purposes only.
These unexpected results occur because primitive types like float and double use binary representations, which can’t exactly represent some decimal values. As a result, rounding with these types can lead to subtle truncation or rounding errors. For example, the value 260.775 cannot be exactly represented as a double. Internally, it might be stored as slightly less than 260.775, so rounding it to two decimal places results in 260.77 instead of 260.78.
These inaccuracies stem from how floating-point numbers are stored in memory. Floating-point values are stored as a combination of a mantissa and an exponent, which allows them to represent a wide range of numbers but at the cost of precision. This makes float and double fast but unreliable for precise values, such as currencies, where accuracy is critical.
In next sections, we’ll dive deeper into how to solve these problems and provide a more reliable way to round numbers.
3. Formatting a Decimal Number
Next, if we want to print a decimal number with n digits after the decimal point, we can simply format the output String:
String formattedValue = String.format("Value with 3 digits after decimal point %.3f", Math.PI);
System.out.println(formattedValue);
// OUTPUTS: Value with 3 digits after decimal point 3.142
Alternatively, we can format the value with the DecimalFormat class:
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.
4. Rounding Doubles with BigDecimal
To round doubles to n decimal places, we can write a helper method:
private static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
There is one important thing to notice in this solution; when constructing BigDecimal, we must always use BigDecimal(String) constructor. This prevents issues with representing inexact values.
We can achieve the same result by using the Apache Commons Math library:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
The latest version can be found here.
Once we add the library to the project, we can use the Precision.round() method, which takes two arguments – value and scale:
Precision.round(PI, 3);
By default, it is using the same HALF_UP rounding method as our helper method; therefore, the results should be the same.
Note that we can change rounding behavior by passing the desired rounding method as a third parameter.
5. Rounding Doubles With DoubleRounder
DoubleRounder is a utility in the decimal4j library. It provides a fast method for rounding doubles from 0 to 18 decimal points.
We can get the library (the latest version can be found here) by adding the dependency to the pom.xml:
<dependency>
<groupId>org.decimal4j</groupId>
<artifactId>decimal4j</artifactId>
<version>1.0.3</version>
</dependency>
Now we can simply use:
DoubleRounder.round(PI, 3);
However, DoubleRounder fails in a few scenarios:
System.out.println(DoubleRounder.round(256.025d, 2));
// OUTPUTS: 256.02 instead of expected 256.03
6. Conclusion
In this article, we covered different techniques for rounding numbers to n decimal places.
We can simply format the output without changing the value, or we can round the variable by using a helper method. We also discussed a few libraries that deal with this problem.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















