Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DecimalFormat df=new DecimalFormat("#.##"); double d = 2.456534; System.out.println("Double upto 2 decimal places: "+df.format(d)); // You can use RoundingMode to round double Up or Down df.setRoundingMode(RoundingMode.DOWN); System.out.println("Double upto 2 decimal places - RoundingMode.DOWN: "+df.format(d)); df.setRoundingMode(RoundingMode.UP); System.out.println("Double upto 2 decimal places - RoundingMode.UP: "+df.format(d)); |
Output:
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.
1 2 3 |
DecimalFormat df=new DecimalFormat("#.##"); |
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:
1 2 3 4 |
double d = 2.456534; System.out.println("Double upto 2 decimal places: "+String.format("%.2f",d)); |
Output:
Further reading:
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:
1 2 3 4 |
double d = 2.456534; System.out.printf("Double upto 2 decimal places: %.2f",d); |
Output:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
BigDecimal bd=new BigDecimal(Double.toString(d)); bd=bd.setScale(2,RoundingMode.HALF_DOWN); System.out.println("Double upto 2 decimal places: "+bd.doubleValue()); // You can use RoundingMode to round double Up or Down BigDecimal bdDown=new BigDecimal(Double.toString(d)); bdDown=bdDown.setScale(2,RoundingMode.DOWN); System.out.println("Double upto 2 decimal places - RoundingMode.DOWN: "+bdDown.doubleValue()); BigDecimal bdUp=new BigDecimal(Double.toString(d)); bdUp=bdDown.setScale(2,RoundingMode.UP); System.out.println("Double upto 2 decimal places - RoundingMode.UP: "+bdUp.doubleValue()); |
Output:
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:
1 2 3 4 5 6 7 |
double d = 2.456534; Formatter formatter = new Formatter(); formatter.format("%.2f", d); System.out.println("Double upto 2 decimal places: " + formatter.toString()); |
Output:
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:
1 2 3 4 5 6 7 8 9 10 |
double d1 = 2.009; double d2 = 2.979; NumberFormat nf= NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); System.out.println("Double d1 upto 2 decimal places: " +nf.format(d1)); System.out.println("Double d2 upto 2 decimal places: " +nf.format(d2)); |
Output:
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
.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.6.1</version> </dependency> |
You can find versions of commons-math over here.
Here is an example:
1 2 3 4 |
Double d= 2.456534; System.out.println("Double upto 2 decimal places: " + Precision.round(d,2)); |
Output:
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.