How to round doubles in java to two decimal places

Whether you are a developer or a non-developer, maybe you have heard of Java. In reality, Java is considered one of the fastest and worth-using computing platforms in the global market. Then, this is the reason why Java is reliable and widely used by a lot of programmers in the world. If you are working with Java but don’t know how to round doubles in Java to two decimal places, this article is for you. Then, through this article, ArrowHiTech will present for you some simple and effective methods to do that. So, let’s explore with us right away! 

Java
Java

Using Math.round(double*100.0)/100.0 to round double to two decimal places Java

Firstly, coming to Java, there are several ways. So, the first one is making use of the method Math.round(double*100.0)/100.0. But, how does it work?Now, let’s explore the following program:

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);

  }

}

When you finish the program above, you will surely get the exact result. Then, let’s see what is it below:

Rounded Double value: 7435.99

How to round doubles in java
Round doubles in java to two decimal places

Also read: java stop program: How to end the program in Java

How to round doubles in java by utilizing BigDecimal?  

We can round the translated BigDecimal to two decimal places by first converting double to BigDecimal and then using the setScale() function. So, in order to know how does this this method work, you should follow the codes below:

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);

  }

}

After that, you will get the following output:

Double value: 4312.186462

Rounded Double value: 4312.19

How to round doubles in java by making use of DecimalFormat

If you want to round double in Java, DecimalFormat is also the ideal choice. Also, want to know how to use it? So, let’s refer to the following code:

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));

  }

}

Finally, do you think this code above works successfully? Then, let’s check the output!

Double value: 6482.236789

Rounded Double value: 6482.24

How to round doubles in java to two decimal places
Methods to round doubles in java to two decimal places

Using Apache Common Math

Coming to the final method of How to round doubles in java that is extremely effective to help you round doubles in Java with ease. Then, it is taking advantage of Apache Common Math. For more details, Apache Common is a specific type of Java math library that is used to round a double to two decimal places. So, let’s have a look at how it’s used in the following scenario.

Besides, this library dependency must be added to the Java project using an xml file:

<dependency>

    <groudId>org.apache.commons</groudId>

    <artifactId>commons-math3</artifactId> 

    <version>3.6.1</version>

</dependency>

Then, let’s examine how the apache library handles round double to two decimal places Java.

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);       

  }

}

As soon as you following the code above, you will achieve the outcome. So, let’s check it below:

Rounded Double value: 9476.24

In conclusion

In short, as you probably, rounding doubles in java to two decimal places isn’t quite difficult. Then, only following one of four methods above, your requirement will be met with ease.

All in all, through this article, ArrowHiTech hopes you will understand how to round doubles in Java and choose the best method to run it. If you can’t apply them successfully or get in any trouble when working with Java, let ArrowHiTech know. With much experience in Kotlin and Java Android App Development Services ,we will surely help you resolve it in the shortest time.

Tags

Share