This Linux Hint tutorial will illustrate the method in Java for rounding a double value to two decimal places.
How to Round a Double to two Decimal Places in Java?
For rounding a double value to two decimal places, you can utilize:
- round() method
- BigDecimal class
- DecimalFormat class
- NumberFormat class
- String format() method
Let’s discuss the working of these methods individually.
Method 1: Round a Double to two Decimal Places by Utilizing Math.round() Method
“Math.round()” is a static method that belongs to the Math class. It rounds the decimal points to the closest whole number. For rounding up to 2 decimal places, the Math.round() method use “(Doublevalue*100.0)/100.0” as an argument.
Syntax
Follow the below-given syntax to round the value up to two decimal places using the Math.round() method:
Example
In this example, we will create a double type variable named “dbl” initialized with the following value:
We will call the “Math.round()” method to round off the double value and then print the updated value using the “System.out.println()” method:
Here is the full sample program you can can compile and run:
The output shows the double value is successfully rounded up to two decimal places:
Original Double value: 5211.1246
Updated rounded Double value is: 5211.12
Let’s look at the other methods to round off the double values to the two decimal places.
Method 2: Round a Double to two Decimal Places by Utilizing BigDecimal Class
We can also round the double values using the “setScale()” method of the BigDecimal class. This class belongs to the “java.math.BigDecimal” package.
Syntax
The following syntax of the BigDecimal.setScale() can be used for the specified purpose:
Here, “dbl” is the BigDecimal class object which will be called the “setScale()” method. This method accepts two parameters, “number” and “RoundingMode”, where number is the integer value that refers to the scale for rounding the decimal value, and the RoundingMode represents the mode of rounding the decimal value.
Example
First, we will create an object of the BigDecimal class “bd” and pass the “dbl” object as an argument and then call the “setScale()” method with the scale “2” and RoundingMode as “HALF-UP”. These arguments will round the Double values two decimal places towards its neighbor:
Then, we will call the “doubleValue()” method with the created BigDecimal class object and store it in a new double type variable name “dbl1”:
Lastly, print the rounded decimal value with the help of the “System.out.println()” method:
Here is the working sample program:
import java.math.RoundingMode;
public class Example {
public static void main(String[] args) {
double dbl = 5212.1246;
System.out.println("Original Double value: "+dbl);
BigDecimal bd = new BigDecimal(dbl).setScale(2, RoundingMode.HALF_UP);
double dbl1 = bd.doubleValue();
System.out.println("Updated rounded Double value is: "+dbl1);
}
}
Output from the console:
Original Double value: 5212.1246
Updated rounded Double value is: 5212.12
Now, let’s try out the next method.
Method 3: Round a Double to two Decimal Places by Utilizing DecimalFormat
The “DecimalFormat” class is utilized to format decimal numbers. This class provides a formatting Pattern to format double to 2 decimal places. It is the subclass of the NumberFormat class.
Syntax
For rounding the double to two decimal number places using the DecimalFormat class, follow the given syntax:
Here, “###.##” represents the format for rounding the number to two decimal places.
Example
We will create an object of the “DecimalFormat” class named “dcf” and pass the mentioned format as an argument:
Print the rounded value by calling the “format()” method and pass the double value “dbl” to it as an argument:
Here is the working Sample Program:
public class Example {
public static void main(String[] args) {
double dbl = 5213.1246;
System.out.println("Original Double value: "+dbl);
DecimalFormat dcf = new DecimalFormat("###.##");
System.out.println("Updated rounded Double value is: "+dcf.format(dbl));
}
}
The output displays the rounded double value up to two decimal places:
Original Double value: 5213.1246
Updated rounded Double value is: 5213.12
Method 4: Round a Double to two Decimal Places by Utilizing NumberFormat Class
The “NumberFormat” is the class that belongs to the java.text.NumberFormat package. It is used to format the decimal numbers with the “setMaximumFractionDigits()” method by passing the required number to round off as an argument.
Syntax
Follow the given syntax of NumberFormat to round a double to two decimal places:
Here, the setMaximumFractionDigits() method will accept the number that tells the scale of decimal points in a double value.
Example
We will first create an instance of the NumberFormat class as “nf”:
Then, we will call the setMaximumFractionDigits() method and pass “2” as an argument that tells the scale of decimal points in a double value:
Lastly, we will print rounded value by calling the “format()” method and passing “dbl” as an argument to it:
Here is the working sample program:
public class Example {
public static void main(String[] args) {
double dbl = 5214.1246;
System.out.println("Original Double value: "+dbl);
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
System.out.println("Updated rounded Double value is: "+nf.format(dbl));
}
}
The output is expected, but this NumberFormat includes commas in the output:
Original Double value: 5214.1246
Updated rounded Double value is: 5,214.12
Method 5: Round a Double to two Decimal Places by Utilizing String format() Method
The “format()” method is the static method of the String class. The double value is rounded up to two decimal places using it. This method acts as a “printf” statement.
Syntax
Follow the below-given syntax to use the String.format() method:
It takes two parameters, “%.2f” and the double value. The first argument represents the required format of the passed double value.
Example
We will call String.format() method by passing a Double class object “dbl” and the “%.2f” format as its arguments:
Here is the working program:
As you can see, the double value is rounded to two decimal places:
Original Double value: 5215.1246
Updated rounded Double value is: 5215.12
We gathered all the essential information related to rounding off the double value up to two decimal places in Java.
Conclusion
For rounding a double value to two decimal places, there are different methods supported by Java language: Math.round() method, String format() method, and other methods of the BigDecimal class, DecimalFormat class, and NumberFormat class. This manual illustrated the methods to round off the double value to two decimal places in Java.