Yes you can do it with String.format:

String result = String.format("%.2f", 10.0 / 3.0);
// result:  "3.33"

result = String.format("%.3f", 2.5);
// result:  "2.500"
Answer from mostar on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - The first argument describes the pattern of how many decimals places we want to see, and the second argument is the given value: double value = 4.2352989244d; assertThat(String.format("%.2f", value)).isEqualTo("4.24"); assertThat(String.for...
Discussions

java - Best way to Format a Double value to 2 Decimal places - Stack Overflow
If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00". ... Sign up to request clarification or add additional context in comments. ... @MubasharAhmad ".99" - the # means "optional zeroes". If you want to preserve the leading zero (ie output "0.99") use the pattern "#0.00" 2014-06-20T02:42:00.317Z+00:00 ... you can use String ... More on stackoverflow.com
🌐 stackoverflow.com
Number formatting with two digits after decimal point in Java - Stack Overflow
I've written code to compute ... two strings and give output like in floating point format with numbers after the decimal point. How can I format the output to display two digits after the decimal point? I don't know how to do this in Java, but I know in C I would use something like .%2.... More on stackoverflow.com
🌐 stackoverflow.com
java - How to format a number 0..9 to display with 2 digits (it's NOT a date) - Stack Overflow
I'd like to always show a number under 100 with 2 digits (example: 03, 05, 15...) How can I append the 0 without using a conditional to check if it's under 10? I need to append the result to anot... More on stackoverflow.com
🌐 stackoverflow.com
formatting - How to print a float with 2 decimal places in Java? - Stack Overflow
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: ... Sign up to request clarification or add additional context in comments. ... Please be carefull as String... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.
🌐
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 - ... In DecimalFormat class, # in Pattern is used to print a digit. Since we require double to 2 decimal places, we used 2 hashes (##) after decimal point (.). We can also use String’s static method format() to print double to 2 decimal places.
🌐
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
* * @author Javin Paul */ public ... float pi = 3.1428733f; // From Java 5, String has a format() method String str = String.format("%.02f", pi); System.out.println("formatted float up to 2 decimals " + str); // If you just ...
🌐
Baeldung
baeldung.com › home › java › java numbers › truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
1 week ago - Firstly, the format we want to apply, and secondly, the arguments referenced by the format. To truncate to two decimal places, we’ll use the format String “%.2f”:
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
import java.util.Calendar; import java.util.Locale; public class TestFormat { public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); // --> "461012" System.out.format("d%n", n); // --> "00461012" System.out.format("%+8d%n", n); // --> " +461012" System.out.format("%,8d%n", n); // --> " 461,012" System.out.format("%+,8d%n%n", n); // --> "+461,012" double pi = Math.PI; System.out.format("%f%n", pi); // --> "3.141593" System.out.format("%.3f%n", pi); // --> "3.142" System.out.format(".3f%n", pi); // --> " 3.142" System.out.format("%-10.3f%n", pi); // --> "3.142"
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › DecimalFormat.html
DecimalFormat (Java Platform SE 8 )
1 week ago - The values are the ones constructed by BigDecimal.BigDecimal(String) for corresponding strings in locale-independent format. The special cases negative and positive infinity and NaN are returned as Double instances holding the values of the corresponding Double constants. DecimalFormat parses all Unicode characters that represent decimal digits, as defined by Character.digit().
🌐
Coderanch
coderanch.com › t › 493635 › java › set-decimals-digits-Doubles
How to set decimals to only 2 digits in Doubles? (Beginning Java forum at Coderanch)
If you don't want to print it, but just return the result formatted in a string, use String.format("%.2f", number); instead (that returns a String). Another, slightly more cumbersome way, is to use class java.text.NumberFormat. ... Thanks Jesper, In my situation I was going to display in inside ...
🌐
W3Schools
w3schools.com › java › ref_string_format.asp
Java String format() Method
String result = String.format("%3$c ... myNumber); System.out.println(result); // Two decimal digits result = String.format("%.2f", myNumber); System.out.println(result); // No decimal digits result = String.format("%.0f", ...
🌐
CodingNConcepts
codingnconcepts.com › java › format-number-to-2-decimal-places
Format number to 2 decimal places in Java - Coding N Concepts
December 1, 2022 - import java.text.DecimalFormat; public class FormatAnyNumber { private static final DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { int num1 = 12345; long num2 = 12345; float num3 = 12345.6f; double num4 = 12345.6789; System.out.println("int: " + df.format(num1)); System.out.println("long: " + df.format(num2)); System.out.println("float: " + df.format(num3)); System.out.println("double: " + df.format(num4)); } } Output int: 12345.00 long: 12345.00 float: 12345.60 double: 12345.68 ·
🌐
Medium
medium.com › @AlexanderObregon › formatting-numbers-for-display-with-decimalformat-in-java-0577c0fe0b52
Formatting Numbers for Display with DecimalFormat in Java
June 27, 2025 - Learn how DecimalFormat works to display numbers with commas, decimals, or currency symbols and how to use it safely with formatting logic in Java.