How To Round Numbers In Java

As you probably already know, it’s not difficult at all to round numbers. However, with so many differences depending on the type of rounding that you are looking for, you may end up finding rounding a bit confusing. 

So, if you always want to make sure that you get the right rounded numbers, then you can definitely learn how to round numbers in Java.


Before we start, we believe that it is important that you know that there are other ways to round your numbers. After all, you can use our free online Rounding Calculator, Microsoft Excel or even Python. And, of course, Java. 

How To Round Numbers In Java

One of the things that you need to know about Java and rounding numbers is that it isn’t that hard. So, ket’s get started. 

Java has two primitive types that can be used for storing decimal numbers: float and double. Double is the type that is used by default:

double PI = 3.1415;

However, both types should never be used for precise values, such as currencies. When you are looking to round currencies or any other precise value, you should use the BigDecimal class instead. 

In case you prefer, just use our rounding calculator.

Formatting A Decimal Number


In case you are simply looking to print a decimal number that has n digits after the decimal point, then you can use the output String

 System.out.printf("Value with 3 digits after decimal point %.3f %n", PI);
// OUTPUTS: Value with 3 digits after decimal point 3.142

In case you prefer, you can also try to format the value with the DecimalFormat class like this: 

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

One of the advantages of using the DecimalFormat is the fact that it allows you to set the exact rounding behavior. Ultimately, this gives you more control over the output than the previous String.format().

Check out our calculator for rounding numbers.

Rounding Doubles with BigDecimal

In case you are looking to round doubles to n decimal laces, then you need to ensure that you write a helper method since this will prevent you from getting inexact values: 

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

Rounding Doubles With DoubleRounder


In case you don’t know, DoubleRounder is a utility that you can find in the decimal4j library. One of the things that we like about it is that you will be using a method that allows you to round doubles from 0 to 18 decimal points in a fast and very precise way. 

To get the library, you just need to add the dependency to the pom.xml:


With this, you can simply use: 

DoubleRounder.round(PI, 3);

However, the DoubleRounder fails in a few scenarios, for example:

  System.out.println(DoubleRounder.round(256.025d, 2));
// OUTPUTS: 256.02 instead of expected 256.03
© 2021 . Powered by WordPress. Theme by Viva Themes.