Round a Double to Two Decimal Places in Java
-
Round of a
double
to Two Decimal Places UsingMath.round(double*100.0)/100.0
-
Round of a
double
to Two Decimal Places UsingBigDecimal
-
Round of a
double
to Two Decimal Places UsingDecimalFormat
-
Round of a
double
to Two Decimal Places UsingApache Common
Math

In the previous tutorial article, we have understood how to convert an Array
to ArrayList
in Java using various methods with detailed examples. We will look at more types of Java usage through different forms of scenario analysis.
In this tutorial article, we will discuss on rounding of a double to two decimal places using Java. There are four ways to round up a double value to two decimal places such as Math.round()
, BigDecimal
using the setScale()
method, DecimalFormat
and Apache Common
library.
Let us discuss each way through examples.
Round of a double
to Two Decimal Places Using Math.round(double*100.0)/100.0
The Math.round()
method is used in Java to round a given number to its nearest integer. Since in this article, we will learn rounding of a double
to 2 decimal places, the application of Math.round()
will include (double*100.0)/100.0
.
Let us follow the below example.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
double d = 7435.9876;
double roundDbl = Math.round(d*100.0)/100.0;
System.out.println("Rounded Double value: "+roundDbl);
}
}
Output:
Rounded Double value: 7435.99
Round of a double
to Two Decimal Places Using BigDecimal
In this way, we can first convert double
to BigDecimal
and then use the setScale()
method to round the converted BigDecimal
to two decimal places. Let us understand the below example.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Main
{
public static void main(String[] args)
{
double val1 = 4312.186462;
System.out.println("Double value: "+val1);
BigDecimal bd = new BigDecimal(val1).setScale(2, RoundingMode.HALF_UP);
double val2 = bd.doubleValue();
System.out.println("Rounded Double value: "+val2);
}
}
Output:
Double value: 4312.186462
Rounded Double value: 4312.19
Round of a double
to Two Decimal Places Using DecimalFormat
We can also round a double
value to two decimal places using DecimalFormat
. Let us discuss in the following example.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;
public class Main
{
public static void main(String[] args)
{
double val1 = 6482.236789;
System.out.println("Double value: "+val1);
DecimalFormat df = new DecimalFormat("###.##");
System.out.println("Rounded Double value (DecimalFormat): "+df.format(val1));
}
}
Output:
Double value: 6482.236789
Rounded Double value: 6482.24
Round of a double
to Two Decimal Places Using Apache Common
Math
A special kind of math library in Java is used to round a double
to two decimal places, which is Apache Common
. Let us discuss its utilization in the following example.
We need to add this library dependency in an xml file within the Java project.
<dependency>
<groudId>org.apache.commons</groudId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
Now let’s check the apache library implementation for rounding a double
to two decimal places.
import java.util.*;
import java.lang.*;
import java.io.*;
import org.apache.commons.math3.util.Precision;
public class Main
{
public static void main(String[] args)
{
double input = 9476.2351;
double roundedDbl = Precision.round(input,2);
System.out.println("Rounded Double value: "+roundedDbl);
}
}
Output:
Rounded Double value: 9476.24
It is recommended to follow the first three ways for simple Java applications.
Related Article - Java Double
- Convert Int to Double in Java
- Double in Java
- Compare Doubles in Java
- Convert long to double in Java
- Float and Double Data Type in Java
- Convert Double to String in Java