How to print a float with 2 decimal places in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java developers often need to work with floating-point numbers and output them in a specific format. One common requirement is to print a float with exactly two decimal places. This can be crucial for applications like financial software, where precision is paramount. Here, we explore various methods to achieve this in Java, from traditional formatting techniques to more modern approaches.
Floating-Point Numbers in Java
Java supports two types of floating-point numbers:
float: A single-precision 32-bit IEEE 754 floating point.double: A double-precision 64-bit IEEE 754 floating point.
For most precision-related operations, double is preferred due to its higher precision. However, when it comes to printing, both types can be formatted similarly.
Techniques to Print with Two Decimal Places
Here's how you can print a float with two decimal places in Java:
1. Using String.format()
The String.format() method is a flexible way to format numbers. It's akin to C's printf.
2. Using System.out.printf()
This method allows you to directly print formatted strings:
3. Using DecimalFormat
For a more customizable approach, DecimalFormat from the java.text package provides a means to format numbers:
4. Using BigDecimal
Though more complex, BigDecimal offers high precision in computations and can round consistently:
Choosing the Right Method
The choice of method depends on various factors such as readability, performance, and precision requirements.
Performance Consideration
String.format()andSystem.out.printf()are typically slower due to their ease of use and flexibility.DecimalFormatoffers a good balance between performance and configurability.BigDecimalis slower but provides the best precision.
Summary Table
Below is a comparative summary of the different approaches:
| Method | Key Points & Usage |
String.format() | Simple & flexible; suitable for quick formatting in a few lines. Not the best choice for repeated operations due to overhead. |
System.out.printf() | Direct printing with format capabilities; ideal for console output. |
DecimalFormat | Highly customizable; moderate performance; worth learning for frequent formatting tasks. |
BigDecimal | Use when precision is critical; slower due to additional computations. |
Conclusion
Printing floating-point numbers with a specific decimal place count is a common task, especially in fields requiring precise data presentation. Java offers multiple strategies to achieve this, ranging from easy-to-use formatting utilities to highly customizable and precise libraries. When working with monetary values or requiring high precision, consider using BigDecimal. For general purposes, anything from String.format() to DecimalFormat will likely suffice. Your choice should be guided by the context in which you are operating as well as any performance considerations.
Related reading
- How to print binary tree diagram in Java?
- How to print color in console using System.out.println?
- How to Print Hexadecimal Numbers in PHP or Java
- How to print out all the elements of a List in Java?
- How to print to the console in Android Studio?
- How to print to the console in Android Studio?
- How to produce a json object message into kafka topic using java(spring)?
- How to programmatically close a JFrame?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.