You can use the printf method, like so:

System.out.printf("%.2f", val);

In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).

There are other conversion characters you can use besides f:

  • d: decimal integer
  • o: octal integer
  • e: floating-point in scientific notation
Answer from Anthony Forloney on Stack Overflow
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
It is a common requirement to format currencies to two decimal places. You can easily achieve this with the Java printf function. Just use %.2f as the format specifier.
Discussions

java - printf %f with only 2 numbers after the decimal point? - Stack Overflow
In my printf, I need to use %f but I'm not sure how to truncate to 2 decimal places: Example: getting 3.14159 to print as: 3.14 More on stackoverflow.com
🌐 stackoverflow.com
Rounding two decimal places using Printf
Look up printf format strings (possibly in C, the syntax is the same). More on reddit.com
🌐 r/learnprogramming
5
1
September 22, 2015
[Grade 11: Computer Science (java)] I need to figure out how to get exactly 2 digits for cents (i.e. $12.5 must be $12.50). However, this must be done with all math and no built-in functions. Any suggestions to improve my code would also be helpful, thanks.
Off-topic Comments Section All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9. OP and Valued/Notable Contributors can close this post by using /lock command I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/HomeworkHelp
13
52
October 19, 2021
printf - Java - How to print values to 2 decimal places - Stack Overflow
I'm coding a simulation of a sports game, and it works fine for the most part; compiles and runs like it should. The directions ask that I I assume that I am supposed to be using printf and %.2f, but More on stackoverflow.com
🌐 stackoverflow.com
September 17, 2013
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
*/ public static void main(String[] args) { double big = 1234.12345; float small = 1234.12345f; System.out.printf("%,.2f :: %,.3f", big, small); /* Example prints: 1,234.12 :: 1234.123 */ } }
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
For example, we might do something similar to the following: double x = 12.345678; System.out.println("Number is approximately " + (int) (x * 100) / 100.0); But this seems a bit "kludgy". Java provides a better way to do the same thing with the "printf" method: double x = 12.345678; ...
🌐
Delft Stack
delftstack.com › home › howto › java › how to print a float with 2 decimal places in java
How to Print a Float With 2 Decimal Places in Java | Delft Stack
February 2, 2024 - "%.2f" specifies that it has 2 decimal places. See the example below. public class SimpleTesting { public static void main(String args[]) { float Pi = 3.1415f; System.out.println(Pi); // Get only 2 decimal points System.out.printf("%.2f", Pi); } } ...
🌐
Java2Blog
java2blog.com › home › number › format double to 2 decimal places in java
Format Double to 2 Decimal Places in Java [ 7 Ways ] - Java2Blog
November 8, 2023 - Here is an example: ... 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 ...
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - System.out.printf("double : %.2f", input); 0 · Reply · Java – How to round double / float value to 2 decimal places · How to calculate monetary values in Java · How to format a double in Java · Java – Convert String to double · Java ...
🌐
Java2Blog
java2blog.com › home › number › 7 ways to print float to 2 decimal places in java
7 ways to print float to 2 decimal places in java - Java2Blog
May 2, 2021 - In this post, we will see how to ... print float to 2 decimal places in java. ... 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:...
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
Thankfully Java provides lots of convenient methods to format a floating point number up to certain decimal places. For example you can use method printf() to format a float or double number to a output stream.
🌐
Reddit
reddit.com › r/learnprogramming › rounding two decimal places using printf
r/learnprogramming on Reddit: Rounding two decimal places using Printf
September 22, 2015 -

Hey guys, so I was just about to finish task one of my programming project when I realized that I needed to stop the ending number at 2 decimal places.

My professor does not wanting us using methods we have not learned yet, and he said that we could use PrintF, however we did not have to use PrintF.

Here is my code that is currently operational:

import java.util.Scanner;

public class A1 {

public static void main(String[] args) 
{
	int ValueOne;
	int ValueTwo;
	int ValueThree;
	float Average;
	
	
	
	Scanner in = new Scanner(System.in);
	
	System.out.println("Enter first value: ");
	ValueOne = in.nextInt();
	System.out.println("You have entered int: "+ValueOne);
	
	System.out.println("Enter second value: ");
	ValueTwo = in.nextInt();
	System.out.println("You have entered int: "+ValueTwo);
	
	System.out.println("Enter third value: ");
	ValueThree = in.nextInt();
	System.out.println("You have entered int: "+ValueThree);
	
	System.out.printf("The average of your three value is : "  + ((ValueOne + ValueTwo + ValueThree) /3));


}

}

I tried researching it before I came here, however did not find anything using PrintF. Any help is greatly appreciated thanks.

🌐
Quora
quora.com › How-do-I-display-a-field-with-two-decimal-places-in-Java
How to display a field with two decimal places in Java - Quora
Answer (1 of 3): You can use the printf method, like so: System.out.printf ("%.2f", val); In short, the %.2f syntax tells Java to return your variable (val ) with 2 decimal places (.2 ) in decimal representation of a floating-point number ( f) from the start of the format specifier ( % ). There a...
🌐
Medium
naveenautomationlabs.medium.com › mastering-system-out-printf-in-java-dcab1af7dd27
Mastering System.out.printf() in Java | by Naveen AutomationLabs | Medium
April 26, 2025 - Reuse previous argument with < flag int n = 123; System.out.printf("20. Hex: %1$x, Decimal: %<d%n", n); // 21. Loop example System.out.println("21. Table:"); for (int i = 1; i <= 5; i++) { System.out.printf(" %d squared is %d%n", i, i * i); } } } 1. The number is: 42 2. Pi to 2 decimal places: 3.14 3.
Top answer
1 of 2
2

You have misunderstood the printf method. You do not concatenate strings the way you do in this line and its successors (reformatted for width reasons):

 System.out.printf("Home team is:" + homeName +
                   " from" + homeLocation +
                   " rated" +  homeTeam.offense +
                   " (offense) +" + homeTeam.defense +
                   " (defense)" + "\n");

This is like the way an old coworker tried to use PreparedStatements to prevent SQL injection attacks, but constructed the query string by concatenation anyway, making the attempt ineffective. Instead, look at the signature of printf:

public PrintWriter format(String format, Object... args)

The first argument is a format string, which contains static text and format directives beginning with %. In typical use, each format directive corresponds to one argument of the method. Replace the interpolated variables with directives.

Strings are usually formatted with %s: s for string. Doubles are usually formatted with %f: f for float (or double). Characters between the % and the letter are options. So, let's replace the strings you interpolated with directives:

"Home team is: "  + "%s" +             // Inserted a space.
" from"           + "%s" +
" rated"          + "%6.2f" +          // Six characters, 2 after the decimal.
" (offense)    +" + "%6.2f" +
" (defense)"      + "%n"               // %n means the appropriate way to get a new line
                                       // for the encoding.

Now we put it all together:

System.out.format("Home team is: %s from %s rated %6.2f (offense) + %6.2f (defense)%n",
                   homeName, homeLocation, homeTeam.offense, homeTeam.defense);

This is a lot simpler. Additionally, another reason to avoid interpolating strings in a format string is that the strings you interpolate may contain a percent sign itself. See what happens if you unguardedly write this:

String salesTax = "5%";
System.out.format("The sales tax is " + salesTax);

That's equivalent to

System.out.format("The sales tax is 5%");

Unfortunately, the percent sign is treated as a format directive, and the format statement throws an exception. Correct is either:

System.out.format("The sales tax is 5%%");

or

String salesTax = "5%";
System.out.format("The sales tax is %s", salesTax);

But now I should ask why you did not take homeName and homeLocation from Team. Certainly they are more relevant to Team than to each other. In fact, you should look up the Formattable interface, and with proper coding you can write:

System.out.format("%s%, homeTeam); 
2 of 2
1

Try this:

public class A {
  public static void main(String[] args) {
    System.out.println(String.format("%.2f", 12.34123123));
  }
}
🌐
iCert Global
icertglobal.com › home › community › how can i format a float to exactly 2 decimal places in java for financial reporting?
How can I format a float to exactly 2 decimal places in Java for financial reporting? | iCert Global
If he uses DecimalFormat, he should probably define it like new DecimalFormat("0.00"). This ensures that even if the number is less than one, like .50, it displays as "0.50" rather than just ".50". It’s a small detail but makes a huge difference in professional software development and data readability. ... For a quick fix, System.out.format("%.2f", myFloat); works exactly like printf and is very easy to implement without importing extra libraries.