How Do I Round a Double to Two Decimal Places in Java?

When working with decimal numbers in Java, you often need to round a double value to two decimal places. This is especially common in financial applications, reports, pricing calculations, and user interfaces where long decimal values can be difficult to read.

In this tutorial, you’ll learn several ways to round a double to two decimal places in Java.

Why Round Decimal Numbers?

Consider the following value:

double number = 12.3456789;

Displaying all decimal digits may not be desirable. Instead, you may want:

12.35

Rounding makes numbers cleaner and easier to understand.

Method 1: Using Math.round()

One of the simplest methods is using Math.round().

double number = 12.3456;

double rounded = Math.round(number * 100.0) / 100.0;

System.out.println(rounded);

Output

12.35

How It Works

  1. Multiply by 100
  2. Round to the nearest whole number
  3. Divide by 100

Example:

12.3456 × 100 = 1234.56
Math.round(1234.56) = 1235
1235 / 100 = 12.35

Method 2: Using String.format()

If you only need formatted output, use String.format().

double number = 12.3456;

String result = String.format("%.2f", number);

System.out.println(result);

Output

12.35

Benefits

  • Easy to use
  • Great for displaying values
  • Common in reports and UI applications

Method 3: Using DecimalFormat

Java provides the DecimalFormat class for number formatting.

import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {

        double number = 12.3456;

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

        System.out.println(df.format(number));
    }
}

Output

12.35

This method is useful when formatting many numbers consistently.

Method 4: Using BigDecimal

For financial calculations, BigDecimal is usually the best choice.

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

public class Main {

    public static void main(String[] args) {

        double number = 12.3456;

        BigDecimal bd =
            new BigDecimal(number);

        bd = bd.setScale(
            2,
            RoundingMode.HALF_UP
        );

        System.out.println(
            bd.doubleValue()
        );
    }
}

Output

12.35

Why Use BigDecimal?

  • High precision
  • Avoids floating-point errors
  • Preferred for money-related applications
See also  How to Make a QScrollArea Fit Optimally to the Height of an Internal QLabel up to a Max Line Count

Comparing the Methods

MethodReturnsBest For
Math.round()doubleQuick rounding
String.format()StringDisplaying values
DecimalFormatStringFormatted output
BigDecimalPrecise numberFinancial calculations

Common Mistake

Many developers try:

double number = 12.3456;

System.out.println(number);

Output:

12.3456

This does not round the value.

You must explicitly apply formatting or rounding logic.

Example: Price Formatting

Suppose you’re displaying a product price:

double price = 199.999;

Using:

System.out.printf(
    "%.2f",
    price
);

Output:

200.00

This provides a cleaner user experience.

Best Practice

For most applications:

double rounded =
    Math.round(value * 100.0)
    / 100.0;

For financial systems:

BigDecimal

is generally the safest and most accurate choice.

Infographic

Conclusion

There are several ways to round a double to two decimal places in Java:

  • Math.round() for quick calculations
  • String.format() for displaying values
  • DecimalFormat for formatting output
  • BigDecimal for precise financial calculations

The best method depends on whether you need a rounded number for calculations or simply want to display it in a user-friendly format.

Happy Coding!

Previous Article

How Can I Avoid Using LLMs as a Software Developer?

Next Article

How to Optimize a Deeply Nested Object Search for Performance?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨