Format Double to 2 Decimal Places in Java

Format double to 2 decimal places in java

1. Introduction

Formatting double to 2 decimal places in Java is a common task that involves rounding, truncating, or displaying a numerical value with a specified number of digits after the decimal point. In this article, we will see different way to format double to 2 Decimal places in Java.

2. Using DecimalFormat Class

DecimalFormat can be used by providing formatting Pattern to format double to 2 decimal places. We can use RoundingMode to specify rounding behavior.
Here is an example:

Output:

Double upto 2 decimal places: 2.46
Double upto 2 decimal places – RoundingMode.DOWN: 2.45
Double upto 2 decimal places – RoundingMode.UP: 2.46

DecimalFormat provides different ways to explicitly set rounding behavior using RoundingMode.. For example, we have used RoundingMode.UP and RoundingMode.DOWN to format double based on different rounding mode.

In DecimalFormat class, # in Pattern is used to print a digit. Since we require double to 2 decimal places, we used 2 hashes (##) after decimal point (.).

3. Using String’s format() Method

We can also use String’s static method format() to print double to 2 decimal places. This method is similar to System.out.printf().
Here is an example:

Output:

Double upto 2 decimal places: 2.46

4. Using System.out.printf()

If we want to print double with 2 decimal places after the decimal point, we can simply format output Double using System.out.printf() method.
Here is an example:

Output:

Double upto 2 decimal places: 2.46

Here, we used %.2f as format specifier.

5. Using BigDecimal

We can convert double to BigDecimal and use BigDecimal‘s setScale() method to format double to 2 decimal places We can use RoundingMode to specify rounding behavior.
Here is an example:

Output:

Double upto 2 decimal places: 2.46
Double upto 2 decimal places – RoundingMode.DOWN: 2.45
Double upto 2 decimal places – RoundingMode.UP: 2.46

Please note that while creating BigDecimal instance, we must always use new BigDecimal(String) to avoid issue with inexact values.

6. Using Formatter Class

We can use java.util.Formatter‘s format() method to format double to 2 decimal places. This is similar to System.out.printf() method.
Here is an example:

Output:

Double upto 2 decimal places: 2.46

7. Using NumberFormat

We can also use NumberFormat‘s setMaximumFractionDigits() to put constraint on number by decimal places and use its format() method to format double to 2 decimal places.
Here is an example:

Output:

Double d1 upto 2 decimal places: 2.01
Double d2 upto 2 decimal places: 2.98

8. Using Apache common library

We can use Precision‘s round() method to format double to 2 decimal places. Precision class belongs to Apache common’s common-math3 library.
Add the following dependency to pom.xml.

You can find versions of commons-math over here.
Here is an example:

Output:

Double upto 2 decimal places: 2.46

9. Conclusion

In this article, we covered different ways to format double to 2 decimal places.
We can use DecimalFormat and BigDecimal classes to customize rounding behavior while formatting double to 2 decimal places. We have also discussed apache common library to do the same.

Was this post helpful?

Leave a Reply

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