Java:
Rounding numbers

How to:

Java offers multiple ways to round numbers. Here’s a quick demo with Math.round(), BigDecimal, and DecimalFormat.

public class RoundingDemo {
    public static void main(String[] args) {
        double num = 123.4567;

        // Using Math.round()
        long roundedNum = Math.round(num);
        System.out.println(roundedNum); // Output: 123

        // Using BigDecimal for more control
        BigDecimal bd = new BigDecimal(num).setScale(2, RoundingMode.HALF_UP);
        double roundedBigDecimal = bd.doubleValue();
        System.out.println(roundedBigDecimal); // Output: 123.46

        // Using DecimalFormat
        DecimalFormat df = new DecimalFormat("#.##");
        String formattedNum = df.format(num);
        System.out.println(formattedNum); // Output: 123.46
    }
}

Deep Dive

Historically, rounding numbers has been essential for analog calculations and has carried over to digital computing for efficiency and accuracy. Rounding errors, like those from floating-point arithmetic, demonstrate that this isn’t a trivial issue — they can accumulatively mess up calculations in, say, aerospace and financial applications.

Beyond Math.round(), you’ve got BigDecimal, which gives you finer control over the scale and rounding mode, and DecimalFormat for when you need to round numbers as part of formatting text output. Alternatives to rounding include flooring, ceiling, and truncating, which are different ways to handle precision and typically handled by various Math methods.

Depending on your use case, the rounding strategy may vary. For example, BigDecimal is go-to for financial calculations, where precision is critical. In contrast, Math.round() is a quick way for general-purpose operations where you’re less picky about the rounding mode.

See Also