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
/* Code example to print a double to two decimal places with Java printf */ System.out.printf("I love %.2f a lot!", Math.PI);
Discussions

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
What is the best way to format a double value in Java to always show two decimal places? - TestMu AI Community
I have a double value, such as 4.0, and I want to ensure it is displayed as 4.00. How can I achieve this using java format double techniques? More on community.testmu.ai
🌐 community.testmu.ai
0
March 17, 2025
Could somebody please explain what the %d means in java?
In a formatted string (mostly likely used in the context of printf()), %d means that an int value is expected at that position in the string, but that the value will be specified in the following comma-separated parameters. For example, doing this: printf("%d + %d = %d", 1, 2, 3); would lead to this output. 1 + 2 = 3 This is useful because it's easier than having to concatenate a whole bunch of values. You can't just type a variable name in the middle of a string, that would lead to a syntax error. If you had done the same thing through concatenation it would look like this: print(1 + " + " + 2 + " = " + 3); Which would you prefer to read/write? More on reddit.com
🌐 r/learnprogramming
41
64
March 13, 2017
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
You generally don't round until you go to display the value. You can use round for that, but since you just want to display it with two digits, you can use string formatting . In your case, something like print(f'{totalPay:.2f}'). .2f is the format specifier, saying round the float (f) to two decimal places (.2). More on reddit.com
🌐 r/learnpython
9
7
January 28, 2020
🌐
Mkyong
mkyong.com › home › java › java – how to round double / float value to 2 decimal places
Java – How to round double / float value to 2 decimal places - Mkyong.com
February 25, 2025 - StringFormatExample.java · package com.mkyong.math.rounding; public class StringFormatExample { public static void main(String[] args) { double input = 1205.6358; System.out.println("Original value : " + input); // Using String.format to round to 2 decimal places System.out.println("Rounded value : " + String.format("%.2f", input)); System.out.format("Rounded value : %.2f", input); } } Output: Original value : 1205.6358 Rounded value : 1205.64 Rounded value : 1205.64 ·
🌐
Coderanch
coderanch.com › t › 721637 › java
%.2f\n (Beginning Java forum at Coderanch)
November 8, 2019 - 2: What you are probably doing viz. displaying the result rounded to 2DP. That only rounds the display, not the calculation.I would do something like this:If you print 123.45 that is six spaces, with 2 after the decimal point: %6.2f. Don't use \n which isn't the correct line end for Windows®: write %n after printf.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-round-a-number-to-n-decimal-places
Java Program to Round a Number to n Decimal Places - GeeksforGeeks
January 22, 2026 - Java · class GFG { public static void main(String[] args) { double number = 3.141341435; System.out.format("%.2f", number); } } Output · 3.14 · DecimalFormat is a subclass of NumberFormat used for formatting decimal numbers with custom patterns. # represents optional digits ·
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
January 15, 2025 - The String.format() method is used with the format specifier %.2f, where 2 represents the number of decimal places and f represents a floating-point number.
Find elsewhere
🌐
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 class JavaDoublePrecision { /* Print Java double to 2 decimals of 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 */ } }
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
double myNumber = 123456.78; String result; // Default result = String.format("%f", myNumber); System.out.println(result); // Two decimal digits result = String.format("%.2f", myNumber); System.out.println(result); // No decimal digits result = String.format("%.0f", myNumber); System.out.println(result); // No decimal digits but keep the decimal point result = String.format("%#.0f", myNumber); System.out.println(result); // Group digits result = String.format("%,.2f", myNumber); System.out.println(result); // Scientific notation with two digits of precision result = String.format("%.2e", myNumber); System.out.println(result); Try it Yourself » ·
🌐
Codecademy
codecademy.com › forum_questions › 51382b87954cc276830008f9
what does "%.2f" mean? | Codecademy
I knew this about the “%” operator ... and java how to program” book. Or you can just search online. And don’t worry, even tho I’m not an expert I’m here to help :) ... % is a symbol for variable and is not a modulo! . and the number fallowing modifies how many digit decimal points you want to print %f string formatting option treats the value as a decimal, and prints it to six decimal places the second % outside of the “ “ sets the variable total to the variable %.2f inside the ...
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - In Java, we can use DecimalFormat("0.00"), String.format("%.2f") or BigDecimal to display double in 2 decimal places.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing …
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
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/java › "%d %.2f \n" ?
r/java on Reddit: "%d %.2f \n" ?
February 26, 2013 -

what does "%d %.2f \n" mean in the following code?

int counter =1;
	
while (counter <= 25){
System.out.printf("%d     %.2f \n", counter,Math.sqrt(counter));
counter += 1;
🌐
Codecademy
codecademy.com › forum_questions › 54469ae052f863fde300e4ce
Please explain this line: print("%.2f" % total) | Codecademy
The % between "%.2f" and total serves as a string interpolation operator in this context. The % inside the string "%.2f" signifies that the .2f is part of a format specifier. Since there is one format specifier here, it must be matched by one value to the right of the % operator.