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 2, 2026 - We can follow that by using setMaximumFractionDigits() to say how many decimal places we want. Finally, because we want to truncate rather than round, we call setRoundingMode() with the argument RoundingMode.DOWN: @Test public void givenADouble_whenUsingNumberFormat_truncateToTwoDecimalPlaces(){ NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); nf.setRoundingMode(RoundingMode.DOWN); double value = 1.55555555; String truncated = nf.format(value); assertEquals("1.55", truncated); double negativeValue = -1.55555555; String negativeTruncated = nf.format(negativeValue); assertEquals("-1.55", negativeTruncated); }
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java – Display double in 2 decimal places | mkyong.com
October 29, 2021 - We also can use String formater / to round the double to 2 decimal places.
Discussions

java - How to convert a double to a string with 2 decimal places? - Stack Overflow
I'm turning this double into a string so I can display it on a TextView. I want the string to have 2 decimal places using String.format, but I don't know where to put it in this line of text. Exa... More on stackoverflow.com
🌐 stackoverflow.com
java - Round a double to 2 decimal places - Stack Overflow
If the value is 200.3456, it should be formatted to 200.34. If it is 200, then it should be 200.00. More on stackoverflow.com
🌐 stackoverflow.com
java - How do I format a double into a string (with only two decimal places)? - Stack Overflow
I am creating an app for Android using Android Studio. I have a block of code that collects some strings, converts them into doubles, puts them into equations, and then puts the answers into TextVi... More on stackoverflow.com
🌐 stackoverflow.com
java - Double.toString with two decimals - Stack Overflow
1490 How to round a number to n decimal places in Java · 1 Decimal Places using double with toString, android · 2 Formatting a double to String using DecimalFormat is throwing IllegalArgumentException More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How can I control the number of decimal places when converting a double to a string in Java?
You can use the String.format() method or DecimalFormat.format() method to control the number of decimal places.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert double to string in java
Turning Double into String: Java Techniques
What's the difference between using String.valueOf() and Double.toString() when converting a double to a string in Java?
Both String.valueOf() and Double.toString() methods can convert a double to a string in Java, and they produce the same result. The main difference lies in the method they belong to. The former is a method of the String class, and the latter is a method of the Double class.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert double to string in java
Turning Double into String: Java Techniques
What happens when a double is converted to a string using the Double.toString() method and the double is Double.NaN or Double.POSITIVE_INFINITY?
If the double value is Double.NaN, the resulting string will be "NaN". If the double value is Double.POSITIVE_INFINITY, the resulting string will be "Infinity".
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert double to string in java
Turning Double into String: Java Techniques
🌐
Baeldung
baeldung.com › home › java › java numbers › convert double to string, removing decimal places
Convert Double to String, Removing Decimal Places | Baeldung
January 8, 2024 - We can confidently use this approach when we’re guaranteed that the double value is within the range of an int. But if our value exceeds that, casting won’t work like we’d want. Now, the remaining approaches aren’t as limited as casting, but they have their own nuances. For example, another approach is to use the format method of the String class. The first parameter of the method specifies that we are formatting a floating point value with zero digits after the decimal point:
🌐
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
You can use any of these technique to pretty print any float or double variable in Java. I personally like to use DecimalFormat for its readability advantage but like SimpleDateFormat, this is also an expensive object and not thread-safe, so use this with caution. One more thing to consider is trailing zeros, if you want trailing zeros e.g. want to print 2 as "2.00" then use String class' format method, otherwise use DecimalFormat's format method.
Find elsewhere
🌐
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 - Double upto 2 decimal places: 2.46 Double upto 2 decimal places – RoundingMode.DOWN: 2.45 Double upto 2 decimal places – RoundingMode.UP: 2.46 · Please note that while creating BigDecimal instance, we must always use new BigDecimal(String) to avoid issue with inexact values. We can use java.util.Formatter‘s format() method to format double to 2 decimal places.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › convert double to string in java
Turning Double into String: Java Techniques
October 1, 2026 - ... In this code, we first declare a double value of 123.45678. We then call String.format() with the format specifier %.2f, which means we want to format a float or double with 2 digits after the decimal point.
Top answer
1 of 13
963

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

For example:

Use with AIround(200.3456, 2); // returns 200.35

Original version; watch out with this

Use with AIpublic 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.)

Use with AIpublic 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:

Use with AI999199.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:

Use with AISystem.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:

Use with AIdouble 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:

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

Or use class DecimalFormat:

Use with AIDecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
🌐
How to do in Java
howtodoinjava.com › home › java basics › round a double to 2 decimal places in java (or n places)
Round a Double to 2 Decimal Places in Java (or N Places)
February 16, 2024 - To round a double to 2 decimal places in Java, we wrap it with BigDecimal.valueOf(), call setScale(2, RoundingMode.HALF_UP) and read the result back with doubleValue(). When we only need the rounded number as text, String.format(“%.2f”, value) ...
🌐
C# Corner
c-sharpcorner.com › home › technologies › java › convert and display double value as string in java
Convert and Display Double Value as String in Java
int count = 10; // Example count value double nota = count * 0.25; txtNota.setText(Double.toString(nota)); ... If count = 10, then nota = 10 * 0.25 = 2.5. The text field txtNota will display 2.5. Java uses the default locale for number formatting, which might result in different decimal separators (e.g., a comma instead of a period in some locales). To explicitly control the format, you can use String.format with a specified Locale.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf
Do not assume every locale uses commas for thousands and periods for decimals. System.out.printf() uses the JVM's default formatting locale unless you provide one explicitly. If an example, test or machine-readable output must always use U.S. punctuation, specify the locale: import java.util.Locale; double value = 1234567.89; System.out.printf( Locale.US, "%,.2f%n", value );
Published: March 30, 2026
🌐
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 decimal and number after the % specify the number of decimals to print. A comma after the % sign adds a grouping separator for large numbers. You can use printf and the %f specifier to format a double to two decimal places of precision.
Published: May 1, 2026
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
January 3, 2016 - So there is no such thing as a "double with two decimal points". I had to redesign the function to return string instead of double... A really, REALLY bad idea. You seem to be hung up on this notion of 2 decimal places, when in fact it's just a formatting issue.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-convert-double-to-string
Java Convert double to String | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Javaprogramto
javaprogramto.com › 2021 › 11 › java-double-2-decimal-places.html
Java Format Double - Double With 2 Decimal Points Examples
Next, create the instance of DecimalFormat class as new DecimalFormat("0.00") and then call the format() method which returns the double value in string with 2 decimal places. Below is the example program. package com.javaprogramto.programs...