No, there is no better way.

Actually you have an error in your pattern. What you want is:

DecimalFormat df = new DecimalFormat("#.00"); 

Note the "00", meaning exactly two decimal places.

If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".

Answer from Bohemian on Stack Overflow
🌐
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
September 24, 2025 - 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”:
🌐
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
The precision of a double in Java is 10-324 decimal places, although true mathematical precision can suffer due to issues with binary arithmetic. To simplify display, you can use %d printf notation to format a Java double’s precision to two ...
🌐
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 – 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 - It is better than Math.Round! ... (12465,2) than it gives 12500 and (12465,3) than it gives 12000 can any one have idea to write such method.in java this question was asked to me at interview. ... Hi Senior programmer, I am glad you shared your ideas online for us to learn. Well done sir. ... DecimalFormat df = new DecimalFormat(“0.00”); double time = 1205.00; time = Double.valueOf(df.format(time)); System.out.println(time); // output 1205.0 but expect 1205.00
🌐
YouTube
youtube.com › watch
How to Format Java double to 2 Decimal Places Without Rounding - YouTube
Learn how to format double values in Java to two decimal places without rounding using DecimalFormat and BigDecimal. Get step-by-step instructions and code e...
Published   April 11, 2025
Views   8
Find elsewhere
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
As others have already said, if you want EXACTLY 2 decimal places, you need a non-floating number type such as BigNumber. On the other hand, if it's only the display value you care about, you can control that using output formatting options.
🌐
Arrowhitech
blog.arrowhitech.com › format-double-to-2-decimal-places-java
Format double to 2 decimal places java: Effective ways to implement it – Blogs | AHT Tech | Digital Commerce Experience Company
To print double to two decimal places, you can utilize the static method format() in String. System.out.printf and this method are similar. ... This is the best way to print double to two decimal places on console if you want to print double to two decimal places.
🌐
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 - // 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(); } } ... // Java Program to Illustrate Precision Setting In Double
🌐
ONEXT DIGITAL
onextdigital.com › home › format double to 2 decimal places java: easy steps to implement it
Format double to 2 decimal places java: Easy Steps to implement it
July 19, 2023 - The double can alternatively be rounded to two decimal places using String formater /. However, we are unable to change the rounding mode in String.format, which always rounds half-up.
🌐
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
By the way, there is subtle difference between formatting floating point numbers using String.format() and DecimalFormat.format(), former will always print trailing zeros even if there is no fractional part. For example if you format 2.00034 up-to two decimal places String's format() method will print "2.00", while format() method of DecimalFormat class will print "2", as shown below :
🌐
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)); } }
🌐
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
The following example creates a DecimalFormat object with the format pattern #.## (where # represents digits) to round the number to 2 decimal places: ... import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double value = 12.34567; DecimalFormat df = new DecimalFormat("#.##"); System.out.println("Rounded value: " + df.format(value)); } }
🌐
TestMu AI Community
community.testmu.ai › ask a question
What is the best way to format a double value in Java to always show two decimal places? - TestMu AI Community
February 7, 2025 - 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?
Top answer
1 of 13
962

Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

For example:

round(200.3456, 2); // returns 200.35

Original version; watch out with this

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

I've been using the above to round "not-too-big" doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it's best to avoid it, since more reliable ways are readily available, with cleaner code too.

So, use this instead

(Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = BigDecimal.valueOf(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

Note that HALF_UP is the rounding mode "commonly taught at school". Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

Of course, if you prefer, you can inline the above into a one-liner:
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

And in every case

Always remember that floating point representations using float and double are inexact. For example, consider these expressions:

999199.1231231235 == 999199.1231231236 // true
1.03 - 0.41 // 0.6200000000000001

For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

System.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

Some excellent further reading on the topic:

  • Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch
  • What Every Programmer Should Know About Floating-Point Arithmetic

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

2 of 13
403

If you just want to print a double with two digits after the decimal point, use something like this:

double value = 200.3456;
System.out.printf("Value: %.2f", value);

If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:

String result = String.format("%.2f", value);

Or use class DecimalFormat:

DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));