# Java: How to Round a Number to N Decimal Places

> Round a number to a specific number of decimal places in Java using BigDecimal for precision, or DecimalFormat and String.format for display

**URL:** https://sentry.io/answers/round-a-number-to-n-decimal-places-in-java/

---

## The Problem

You don't know how to round a value to a specific number of decimal places in Java.

## The Solution

There are multiple ways to round a number in Java, depending on the precision you require and how you plan to use the result.

- The `BigDecimal` class is best suited to financial and high-precision applications.
- The `DecimalFormat` class is better suited to user-facing applications.
- The `String.format()` method is useful when formatting numbers for display purposes.
- Custom utility methods can be used to create precise and reusable rounding methods.

### Use the `BigDecimal` Class

The Java `BigDecimal` class gives you precise control over rounding while avoiding the issues caused by floating-point arithmetic. You can specify both the number of decimal places and the rounding mode.

For example, the following code creates a `BigDecimal` object from the `double` value and uses the `setScale()` method to specify two decimal places and the `HALF_UP` rounding mode:

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

public class Main {
    public static void main(String[] args) {
        double value = 12.34567;
        int decimalPlaces = 2;

        BigDecimal bd = new BigDecimal(Double.toString(value));
        bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);

        System.out.println("Rounded value: " + bd);
    }
}
```

The `HALF_UP` rounding mode is a common rounding strategy in which values are rounded up when the digit after the rounding place is `5` or greater.

In this case, the output will be:

```
Rounded value: 12.35
```

The precision offered by the `BigDecimal` class makes this approach suitable for financial and high-precision applications.

### Use the `DecimalFormat` Class

You can use the Java `DecimalFormat` class to define a format pattern that rounds numbers to a specific number of decimal places for display.

The following example creates a `DecimalFormat` object with the format pattern `#.##` (where `#` represents digits) to round the number to 2 decimal places:

```java
import java.text.DecimalFormat;

public class Main {
	public static void main(String[] args) {
        double value = 12.34567;
        DecimalFormat df = new DecimalFormat("#.##");

        System.out.println("Rounded value: " + df.format(value));
    }
}
```

The output will be:

```
Rounded value: 12.35
```

Because the `format()` method returns the rounded number as a `String`, this approach is best used to format numbers for user-facing applications. It is not ideal for calculations, as the string result is less precise than other options.

### Use the `String.format()` Method

Like the `DecimalFormat` class, the Java `String.format()` method allows you to format a value to a specific number of decimal places and outputs it as a string.

Note the different syntax in the following example:

```java
public class Main {
	public static void main(String[] args) {
        double value = 12.34567;

        String roundedValue = String.format("%.2f", value);
        System.out.println("Rounded value: " + roundedValue);
    }
}
```

The `String.format()` method is used with the format specifier `%.2f`, where `2` represents the number of decimal places and `f` represents a floating-point number. The result is a string with the value rounded to 2 decimal places.

The output will be:

```
Rounded value: 12.35
```

The `String.format()` method is easy and useful when you need to display formatted numbers, but like `DecimalFormat`, the result is a string and not suitable for further numerical calculations.

### Use Custom Utility Methods

To make the rounding process reusable, you can create a custom utility method that encapsulates one of the approaches shown above.

The following example uses the `BigDecimal` class:

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

public class Main {
    public static void main(String[] args) {
        double value = 12.34567;
        double roundedValue = roundToNDecimalPlaces(value, 2);
        System.out.println("Rounded value: " + roundedValue);
    }

    public static double roundToNDecimalPlaces(double value, int decimalPlaces) {
        BigDecimal bd = new BigDecimal(Double.toString(value));
        bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
}
```

This custom utility method creates a reusable `roundToNDecimalPlaces()` method that takes the value and the number of decimal places as parameters, uses `BigDecimal` to round the number, and returns the result as a `double`.

## Further Reading

- The [Java `BigDecimal` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/math/BigDecimal.html) documentation
- The [Java `DecimalFormat` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/text/DecimalFormat.html) documentation
- The [Java `String.format()` method](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/Formatter.html) documentation
- The [Java `RoundingMode` class](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/math/RoundingMode.html) documentation

---

*Source: [sentry.io/answers/round-a-number-to-n-decimal-places-in-java/](https://sentry.io/answers/round-a-number-to-n-decimal-places-in-java/)*
