Java program to round a number to n decimal places

Java program to round a number to n decimal places:

There are different ways to round a number to n decimal places. In this post, we will learn how to do that with explanations and examples for each.

Math.round():

The Math.round() method can round a number to the closest value. It takes a double or float value as its parameter and returns the rounded value.

We can use this method to round a number to n decimal places. We have to multiply the number by 10^n, find the value of Math.round() of this value and divide it by 10^n. It will give the round value to n decimal places.

Let’s take a look at the below example:

class Main {

    public static void main(String[] args) {
        double[] sampleNumbers = {1, 12.22345, 100.0, 134.2298, 12.1, 265.335};

        for (double n : sampleNumbers) {
            double result = Math.round(n * 100.0) / 100.0;
            System.out.println(n + " : " + result);
        }
    }
}

Here, we have declared an array of double values, sampleNumbers. The for loop iterate over these numbers and finds the rounded value to the second decimal place.

If you run this program, it will print:

1.0 : 1.0
12.22345 : 12.22
100.0 : 100.0
134.2298 : 134.23
12.1 : 12.1
265.335 : 265.33

This is not a recommended way to round a number as fails in many cases. Even in the above example, it got failed for 265.335. The result should be 265.34, but it printed 265.33.

This method is added only to show you the drawback.

With DecimalFormat class:

The DecimalFormat class is used for formatting numbers in decimal. First of all, we have to create one object of DecimalFormat. It takes the format pattern as its parameter.

We have to call the format method on this object and pass the double value to this method as the parameter. It will return the formatted value.

Let’s take a look at the below example:

import java.text.DecimalFormat;

class Main {

    public static void main(String[] args) {
        double[] sampleNumbers = {1, 12.22345, 100.0, 134.2298, 12.1, 265.335};

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

        for (double n : sampleNumbers) {
            String result = decimalFormat.format(n);
            System.out.println(n + " : " + result);
        }
    }
}

It will print:

1.0 : 1
12.22345 : 12.22
100.0 : 100
134.2298 : 134.23
12.1 : 12.1
265.335 : 265.33

As you can see here, it fails in some cases like 265.335.

With BigDecimal:

Another way is to convert the double value to a BigDecimal object. We can round that value to any decimal places we want.

import java.math.BigDecimal;
import java.math.RoundingMode;

class Main {

    public static void main(String[] args) {
        double[] sampleNumbers = {1, 12.22345, 100.0, 134.2298, 12.1, 265.335};

        for (double n : sampleNumbers) {
            BigDecimal bDecimal = new BigDecimal(n);
            BigDecimal result = bDecimal.setScale(2, RoundingMode.HALF_UP);
            System.out.println(n + " : " + result);
        }
    }
}

This will print the below output:

1.0 : 1.00
12.22345 : 12.22
100.0 : 100.00
134.2298 : 134.23
12.1 : 12.10
265.335 : 265.33

This method also fails with 265.335.

You might also like: