In this post, we will see how to format double to 2 decimal places.
There are multiple ways to format double to 2 decimal places. Let’s go through them.
Table of Contents
Using String’s format() method
You 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 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class StringFromatformatDouble { public static void main(String[] args) { double d = 2.456534; System.out.println("Double upto 2 decimal places: "+String.format("%.2f",d)); } } |
Output:
Using System.out.printf
If you want to print double to 2 decimal places, this is best way to print double to 2 decimals on console.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class PrintfformatDouble { public static void main(String[] args) { double d = 2.456534; System.out.printf("Double upto 2 decimal places: %.2f",d); } } |
Output:
Using Formatter
You 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 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.Formatter; public class FormatterformatDouble { public static void main(String[] args) { double d = 2.456534; Formatter formatter = new Formatter(); formatter.format("%.2f", d); System.out.println("Double upto 2 decimal places: " + formatter.toString()); } } |
Output:
Using BigDecimal
You can convert double to BigDecimal and use BigDecimal
’s setScale()
method to format double to 2 decimal places You 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 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class BigDecimalformatDouble { public static void main(String[] args) { double d = 2.456534; BigDecimal bd=new BigDecimal(d).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(d).setScale(2,RoundingMode.DOWN); System.out.println("Double upto 2 decimal places - RoundingMode.DOWN: "+bdDown.doubleValue()); BigDecimal bdUp=new BigDecimal(d).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
Using DecimalFormat
DecimalFormat can be used by providing formatting Pattern to format double to 2 decimal places. You 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 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalFormatformatDouble { public static void main(String[] args) { 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
Using NumberFormat
You 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 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.text.NumberFormat; import java.util.Formatter; public class NumberFormatformatDouble { public static void main(String[] args) { 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
Using Apache common library
You 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 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import org.apache.commons.math3.util.Precision; public class ApacheCommonPrecisionMain { public static void main(String[] args) { Double d= 2.456534; System.out.println("Double upto 2 decimal places: " + Precision.round(d,2)); } } |
Output:
That’s all about How to format double to 2 decimal places