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

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
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
Truncate to 2 decimal places in Java
The most obvious solution is to multiply by 100, cast to int and multiply by 0.01 but I'm not sure if this could lead to small rounding errors (something I normally don't want to rule out when doing arithmetic with floating point numbers). More on reddit.com
🌐 r/learnprogramming
6
1
December 22, 2021
[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
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
Interestingly, it is possible to creatively use casting and promotion to print a "double" value with only two decimal places. 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; System.out.printf("Number is approximately %.2f", x); //prints "Number is approximately 12.35" System.out.printf( format, item1, item2, ..., itemK ); Here, "format" is a string to print with "format specifiers" (each consisting of a percent sign and a conversion code) that tell Java where and how to display "item1", "item2", etc...
🌐
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.

🌐
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 */ } }
Find elsewhere
🌐
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); ...
🌐
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.
🌐
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 - 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.
🌐
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.
🌐
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.
🌐
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 ...
🌐
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...
🌐
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 - There are multiple ways to 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.
🌐
YouTube
youtube.com › watch
How to format a Java double with printf example - YouTube
Do you need to format a double in Java to two decimal places with the printf function? Need to specify the width, decimal precision or even the use of a comm...
Published   June 13, 2022