One of the way would be using NumberFormat.

NumberFormat formatter = new DecimalFormat("#0.00");     
System.out.println(formatter.format(4.0));

Output:

4.00

Answer from kosa on Stack Overflow
🌐
Mkyong
mkyong.com › home › java › how to format a double in java
How to format a double in Java - Mkyong.com
July 23, 2020 - For String.format, we can use %f to format a double, review the following Java example to format a double. ... package com.mkyong.io.utils; import java.util.Locale; public class FormatDouble1 { public static void main(String[] args) { String ...
Discussions

String.format() to format double in Java - Stack Overflow
How can I use String.format(format, args) to format a double like below? 2354548.235 -> 2,354,548.23 More on stackoverflow.com
🌐 stackoverflow.com
How to return a formatted double
What you need to do is manipulate the double in such a way that you remove any value after 2 decimal places. For example, if your double was 123.4567 and you want the final manipulated value to be 123.45 then you could simply multiply the double by 100 to shift the decimal's placement, call Math.round() to remove all value after the decimal, then divide by 100 to shift the decimal back to it's proper position. This is only one implementation, you could do it many ways. Most importantly, think about whether or not you really want to lose precision. If you only want to show two decimal places when printing the value, then using printf makes sense and allows the value to remain as precise as possible. More on reddit.com
🌐 r/javahelp
6
0
June 1, 2021
java - Best way to Format a Double value to 2 Decimal places - Stack Overflow
I am dealing with lot of double values in my application, is there is any easy way to handle the formatting of decimal values in Java? Is there any other better way of doing it than DecimalForm... More on stackoverflow.com
🌐 stackoverflow.com
[Java] Rounding a double to two decimal places
System.out.printf("%.2f", 4.567823); More on reddit.com
🌐 r/learnprogramming
3
0
April 28, 2013
🌐
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
To use Java printf to format double and float values with decimal place precision, follow these two rules: Use %f as the double specifier in the text string.
🌐
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - In this tutorial, we’ll learn about the different approaches to number formatting in Java, and how to implement them. The String#format method is very useful for formatting numbers. The method takes two arguments. 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.format("%.3f", value)).isEqualTo("4.235");
🌐
Delft Stack
delftstack.com › home › howto › java › java format double
How to Format Double Value in Java | Delft Stack
February 2, 2024 - In this example, we use the DecimalFormat class to format double type value in the specified format. For example, to format a double value to three decimal places, we use the format() method and pass the DecimalFormat constructor’s format style.
🌐
TutorialsPoint
tutorialspoint.com › format-double-type-in-java
Format double type in Java
June 26, 2020 - Let’s say we have the following three values − Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log.
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
A double in Java is a 64-bit number, and the 64-bit precision of a double never changes within a Java program. However, to format the output of a double to two decimal places, simply use the printf method and %.2f as the specifier.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Double.html
Double (Java Platform SE 8 )
April 21, 2026 - The argument is considered to be ... "double format" bit layout. If the argument is 0x7ff0000000000000L, the result is positive infinity. If the argument is 0xfff0000000000000L, the result is negative infinity. If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish ...
🌐
Reddit
reddit.com › r/javahelp › how to return a formatted double
r/javahelp on Reddit: How to return a formatted double
June 1, 2021 -

I have a method that returns a double, however, I would like have it formatted to 2 decimal places. Is there a way to do this without using toString()? When I run the main method I System.out.println(driver.getCarValue()); and it prints to the console the double in scientific notation. So, I included a System.out.printF() and formatted as per code block and when I run the main method and write driver.getCarValue(); I am able to return the double formatted as I like. I am wondering if I did this correctly or if the way that I have setup my method is not the proper way I should be coding it?

public double getCarValue() {
    double value = 0;
    for (Cars totalValue : car.values()) {
	value = value + totalValue.getPrice();
	}
	System.out.printf("The total value is: $%.2f", value);
	return value;
}
Top answer
1 of 3
2
What you need to do is manipulate the double in such a way that you remove any value after 2 decimal places. For example, if your double was 123.4567 and you want the final manipulated value to be 123.45 then you could simply multiply the double by 100 to shift the decimal's placement, call Math.round() to remove all value after the decimal, then divide by 100 to shift the decimal back to it's proper position. This is only one implementation, you could do it many ways. Most importantly, think about whether or not you really want to lose precision. If you only want to show two decimal places when printing the value, then using printf makes sense and allows the value to remain as precise as possible.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › numberintro.html
Formatting Numbers
DecimalFormat, to format primitive-type numbers, such as double, and their corresponding wrapper objects, such as Double. The NumberFormat and DecimalFormat classes are in the java.text package.
🌐
Edureka Community
edureka.co › home › community › categories › java › formatting double value in java
Formatting Double Value in Java | Edureka Community
May 15, 2018 - I am dealing with lot of double values in my application, is there is any easy way to handle the formatting of ... to 35.70 3.0 to 3.00 9 to 9.00
🌐
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. Here is an example: ... Here, we used %.2f as format specifier.
🌐
CodeGym
codegym.cc › java blog › java numbers › java double keyword
Java double keyword
February 19, 2025 - The double type is twice as large as float: 8 bytes versus 4. It is also called a double precision real number. Of the 64 bits reserved for a double number, 1 is signed bit, 11 bits are for exponent and 52 bits are for significand.
🌐
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
For example you can use method printf() to format a float or double number to a output stream. However, it does not return a String. In JDK 1.5, a new static method format() was added to the String class, which is similar to printf(), but returns ...
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - package com.mkyong.math.rounding; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalExample { private static final DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { double input = 3.14159265359; System.out.println("double : " + input); System.out.println("double : " + df.format(input)); //3.14 // DecimalFormat, default is RoundingMode.HALF_EVEN df.setRoundingMode(RoundingMode.DOWN); System.out.println("\ndouble (RoundingMode.DOWN) : " + df.format(input)); //3.14 df.setRoundingMode(RoundingMode.UP); System.out.println("double (RoundingMode.UP) : " + df.format(input)); //3.15 } }
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Double.html
Double (Java Platform SE 6)
The argument is considered to be ... "double format" bit layout. If the argument is 0x7ff0000000000000L, the result is positive infinity. If the argument is 0xfff0000000000000L, the result is negative infinity. If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-precision-for-double-values-in-java
How to Set Precision For Double Values in Java? - GeeksforGeeks
July 12, 2025 - From the above output, it is clear that precision of 20 digits is been carried out for the first entry whereas precision to 5 digits is carried out on input double value. ... // Demonstrating the precision modifier import java.util.*; class GFG { public static void main (String[] args) { Formatter fm=new Formatter(); // Format 4 decimal places fm.format("%.4f", 123.1234567); System.out.println(fm); fm.close(); //Format 2 decimal places in a 16 character field fm=new Formatter(); fm.format(".2e",123.1234567); System.out.println("GFG!"); fm.close(); //Display atmost 15 characters in a string fm=new Formatter(); fm.format("%.15s", "Learning with Gfg is easy quick"); System.out.println(fm); fm.close(); } }
🌐
Coderanch
coderanch.com › t › 689796 › java › format-double-decimal
format a double decimal (Beginning Java forum at Coderanch)
January 21, 2018 - Now, back to my original question, on the decimal formatting to 3 digits, line 17 is supposed to return the bmi with 3 decimal places, however I only receive the error "The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, double)" Any ideas what I am doing wrong? ... If the 4 digit number is created in one statement, there is not need for a loop. ... Now you don't need the loop, but you are ruling out id's that start with a zero. A java 8 stream version could be: