How to round a number to n decimal places in Java?

How can I round a number to n decimal places in Java?

I need a method to round a double in Java using the half-up rounding method, meaning if the digit to be rounded is 5, it always rounds up. Additionally, I want to ensure that only significant digits are displayed—there should be no trailing zeroes.

I have tried String.format("%.5g%n", value), but it sometimes retains unnecessary zeroes. I also tested DecimalFormat df = new DecimalFormat("#.#####");, but it uses half-even rounding instead of half-up.

For example, I want these results:

  • 0.912385 → 0.91239
  • 0.912300 → 0.9123

What is the best approach to achieve this in Java rounding?

If you need precise rounding, BigDecimal is the way to go! I’ve worked with financial calculations, and trust me, this saves you from tricky rounding errors.

Using BigDecimal for Precise Rounding

If you want full control over rounding behavior, BigDecimal is your best bet. It ensures exact precision, especially when dealing with currency or scientific calculations.

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

public class RoundNumber {
    public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException("Decimal places must be non-negative");
        
        BigDecimal bd = new BigDecimal(value).setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }

    public static void main(String[] args) {
        System.out.println(round(0.912385, 5));  // Output: 0.91239
        System.out.println(round(0.912300, 5));  // Output: 0.9123
    }
}

:white_check_mark: Why use this?

:heavy_check_mark: Precise control over rounding mode (HALF_UP ensures 5 rounds up).

:heavy_check_mark: No trailing zeroes in the final result.

:rotating_light: When to avoid?

If you want a simpler approach without dealing with BigDecimal, check out the next method.

That’s great for accuracy, @kumari_babitaa , but sometimes, all you need is a clean, formatted output without trailing zeroes. I usually prefer DecimalFormat when displaying numbers.

Using DecimalFormat for Custom Formatting

If you’re working on formatted output (like UI displays), DecimalFormat can round numbers while automatically removing unnecessary zeroes.

import java.text.DecimalFormat;

public class RoundNumber {
    public static String round(double value) {
        DecimalFormat df = new DecimalFormat("#.#####");
        return df.format(value);
    }

    public static void main(String[] args) {
        System.out.println(round(0.912385));  // Output: 0.91239
        System.out.println(round(0.912300));  // Output: 0.9123
    }
}

:white_check_mark: Why use this?

:heavy_check_mark: Automatically removes trailing zeroes.

:heavy_check_mark: Simple and effective for formatted output.

:rotating_light: When to avoid?

If you need strict precision control for calculations (use BigDecimal instead).

Both approaches are great, but what if you just need a quick and lightweight way to round numbers in Java? Math.round does the job with minimal effort!

Using Math.round with Scaling for Simplicity

For a straightforward method without extra libraries, Math.round can work with a scaling trick.

public class RoundNumber {
    public static double round(double value, int places) {
        double scale = Math.pow(10, places);
        return Math.round(value * scale) / scale;
    }

    public static void main(String[] args) {
        System.out.println(round(0.912385, 5));  // Output: 0.91239
        System.out.println(round(0.912300, 5));  // Output: 0.9123
    }
}

:white_check_mark: Why use this?

:heavy_check_mark: Lightweight and efficient (uses Math.round).

:heavy_check_mark: Works well for basic rounding needs.

:rotating_light: When to avoid?

If you need removal of trailing zeroes, use DecimalFormat instead.