Java

How to Round a Double to two Decimal Places in Java

There exists a chance that you only need two decimal places of a created Double variable, in some cases, such as representing units of currency. Programmers typically use the “round()” method to round two decimal places to discover approximate decimal number values. Also, decimal values or figures are typically rounded off when performing computations to make the process simpler.

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:

Math.round(Doublevalue*100.0)/100.0

Example

In this example, we will create a double type variable named “dbl” initialized with the following value:

double dbl = 5211.1246;
System.out.println("Original Double value: "+dbl);

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:

double roundVal = Math.round(dbl*100.0)/100.0;
System.out.println("Updated rounded Double value: "+roundVal);

Here is the full sample program you can can compile and run:

public class Example {

        public static void main(String[] args) {
                double dbl = 5211.1246;
                System.out.println("Original Double value: "+dbl);
                double roundVal = Math.round(dbl*100.0)/100.0;
                System.out.println("Updated rounded Double value is: "+roundVal);
        }
}

The output shows the double value is successfully rounded up to two decimal places:

linuxhint@DESKTOP-XXXXXX:~$ java Example
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:

BigDecimal(dbl).setScale(number, RoundingMode.HALF_UP);

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:

BigDecimal bd = new BigDecimal(dbl).setScale(2, RoundingMode.HALF_UP);

Then, we will call the “doubleValue()” method with the created BigDecimal class object and store it in a new double type variable name “dbl1”:

double dbl1 = bd.doubleValue();

Lastly, print the rounded decimal value with the help of the “System.out.println()” method:

System.out.println("Updated rounded Double value: "+dbl1);

Here is the working sample program:

import java.math.BigDecimal;
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:

linuxhint@DESKTOP-XXXXXX:~$ java Example
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:

DecimalFormat("###.##");

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:

DecimalFormat dcf = new DecimalFormat("###.##");

Print the rounded value by calling the “format()” method and pass the double value “dbl” to it as an argument:

System.out.println("Updated rounded Double value: "+dcf.format(dbl));

Here is the working Sample Program:

import java.text.DecimalFormat;

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:

linuxhint@DESKTOP-XXXXXX:~$ java Example
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:

setMaximumFractionDigits(number);

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”:

NumberFormat nf= NumberFormat.getInstance();

Then, we will call the setMaximumFractionDigits() method and pass “2” as an argument that tells the scale of decimal points in a double value:

nf.setMaximumFractionDigits(2);

Lastly, we will print rounded value by calling the “format()” method and passing “dbl” as an argument to it:

System.out.println("Updated rounded Double value: "+nf.format(dbl));

Here is the working sample program:

import java.text.NumberFormat;

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:

linuxhint@DESKTOP-XXXXXX:~$ java Example
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:

String.format("%.2f", doublevalue)

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:

System.out.println("Updated rounded Double value: "+String.format("%.2f",dbl));

Here is the working program:

public class Example {

      public static void main(String[] args) {
           double dbl = 5215.1246;
           System.out.println("Original Double value: "+dbl);
           System.out.println("Updated rounded Double value is: "+String.format("%.2f", dbl));
      }
}

As you can see, the double value is rounded to two decimal places:

linuxhint@DESKTOP-XXXXXX:~$ java Example
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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.