In this post, we will see how to print float to 2 decimal places in java.
There are multiple ways to how to print float to 2 decimal places in java.
Table of Contents
1. Using System.out.printf
In case, if you just want to print float to 2 decimal places, this is best method. You can simply use System.out.printf
with format %.2f
.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog.entry; public class PrintfFloatMain { public static void main(String[] args) { float f= 1.232312f; // Print float to 2 decimal places using printf System.out.printf("Float upto 2 decimal places: %.2f",f); } } |
Output:
2. Using String’s format() method
String class also provide static method format()
which you can use to print float to 2 decimal places in java. This works similar to System.out.printf
.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog.entry; public class StringFormatPrintFloat { public static void main(String[] args) { float f= 1.232312f; System.out.println("Float upto 2 decimal places: "+String.format("%.2f",f)); } } |
Output:
3. Using Formatter
java.util.Formatter
class provides format() method which you can use to print float to 2 decimal places in java. This works similar to System.out.printf
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog.entry; import java.util.Formatter; public class FormatterPrintFloat { public static void main(String[] args) { float f= 1.232312f; Formatter fm = new Formatter(); fm.format("%.2f", f); // Print float to 2 decimal places using Formatter System.out.println("Float upto 2 decimal places: " + fm.toString()); } } |
Output:
4. Using NumberFormat
You can also use NumberFormat
‘s setMaximumFractionDigits()
to put constraint on number of decimal places and use its format()
method to display 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 22 |
package org.arpit.java2blog.entry; import java.text.NumberFormat; import java.util.Formatter; public class NumberFormatPrintFloat { public static void main(String[] args) { float f1 = 1.008f; float f2 = 1.868f; NumberFormat numberFormat= NumberFormat.getInstance(); numberFormat.setMaximumFractionDigits(2); // Print float to 2 decimal places using NumberFormat System.out.println("Float f1 upto 2 decimal places: " +numberFormat.format(f1)); System.out.println("Float f2 upto 2 decimal places: " +numberFormat.format(f2)); } } |
Output:
Float f2 upto 2 decimal places: 1.87
5. Using BigDecimal
You can convert float to BigDecimal and use BigDecimal
‘s setScale()
method to print float 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 22 23 24 25 |
package org.arpit.java2blog.entry; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class BigDecimalDisplayFloat { public static void main(String[] args) { float f= 1.232312f; BigDecimal bd=new BigDecimal(f).setScale(2,RoundingMode.HALF_DOWN); // Print float to 2 decimal places using BigDecimal System.out.println("Float upto 2 decimal places: "+bd.doubleValue()); // You can use RoundingMode to round double Up or Down BigDecimal bdDown=new BigDecimal(f).setScale(2,RoundingMode.DOWN); System.out.println("Float upto 2 decimal places - RoundingMode.DOWN: "+bdDown.doubleValue()); BigDecimal bdUp=new BigDecimal(f).setScale(2,RoundingMode.UP); System.out.println("Float upto 2 decimal places - RoundingMode.UP: "+bdUp.doubleValue()); } } |
Output:
Float upto 2 decimal places – RoundingMode.DOWN: 1.23
Float upto 2 decimal places – RoundingMode.UP: 1.24
6. Using DecimalFormat
You can provide formatting pattern to DecimalFormat class to print float to 2 decimal places. Here, RoundingMode can be used to control 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 24 25 |
package org.arpit.java2blog.entry; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalFormatPrintFloat { public static void main(String[] args) { DecimalFormat df=new DecimalFormat("#.##"); float f= 1.232312f; // Print float to 2 decimal places using DecimalFormat System.out.println("Float upto 2 decimal places: "+df.format(f)); // Use RoundingMode to up and down the float df.setRoundingMode(RoundingMode.DOWN); System.out.println("Float upto 2 decimal places using RoundingMode.DOWN: "+df.format(f)); df.setRoundingMode(RoundingMode.UP); System.out.println("Float upto 2 decimal places using RoundingMode.UP: "+df.format(f)); } } |
Output:
Float upto 2 decimal places using RoundingMode.DOWN: 1.23
Float upto 2 decimal places using RoundingMode.UP: 1.24
7. Using Apache common library
You can use Precision
‘s round()
method to display double to 2 decimal places. Precision
class belongs to Apache common’s common-math3
library.
You need to add 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 change 3.6.1
to latest version if you want from here.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog.entry; import org.apache.commons.math3.util.Precision; public class ApacheCommonPrecisionFloatMain { public static void main(String[] args) { float f= 1.232312f; // Print float to 2 decimal places using Apache common Precision System.out.println("Print float upto 2 decimal places: " + Precision.round(f,2)); } } |
Output:
That’s all about How to print float to 2 decimal places