Use DecimalFormat: new DecimalFormat("#.0#####").format(d).

This will produce numbers with 1 to 6 decimal digits.

Since DecimalFormat will use the symbols of the default locale, you might want to provide which symbols to use:

//Format using english symbols, e.g. 100.0 instead of 100,0
new DecimalFormat("#.0#####", DecimalFormatSymbols.getInstance( Locale.ENGLISH )).format(d)

In order to format 100.0 to 100, use the format string #.######.

Note that DecimalFormat will round by default, e.g. if you pass in 0.9999999 you'll get the output 1. If you want to get 0.999999 instead, provide a different rounding mode:

DecimalFormat formatter = new DecimalFormat("#.######", DecimalFormatSymbols.getInstance( Locale.ENGLISH ));
formatter.setRoundingMode( RoundingMode.DOWN );
String s = formatter.format(d);
Answer from Thomas on Stack Overflow
🌐
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(); } }
🌐
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.
🌐
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 static void main(String[] ... a Java double to two decimals of precision works as follows: The %f specifier represents a double or float value to format....
🌐
Reddit
reddit.com › r/javahelp › how to convert string to double without losing precision after decimal point?
How to convert string to double without losing precision after decimal point? : r/javahelp
July 21, 2021 - The long answer is that 5.0 and 5.00000 are the same thing. Internally double has more precision, but when you print a double it only prints with the amount of precision you need. The answer is to print the decimal with a format string as shown in the second line below.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-double-type-variables-to-string
Java Program to convert double type variables to string | Vultr Docs
December 10, 2024 - This statement makes use of Java’s String.format() method, where "%.2f" specifies that the double should be formatted to two decimal places. The output here is "3.14", demonstrating a straightforward way to tailor the output precision.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 385764 › java › covert-String-double-loosing-precision
How to covert a String value to double without loosing the precision (Java in General forum at Coderanch)
Well, it really has nothing to do with precision but rather with presentation (they're not the same). If the OP wants to have 4 leading numbers then the obvious choice would be to use java.text.DecimalFormat with the pattern: "0.0000": ... Originally posted by Campbell Ritchie: We're not here to do maths . . . It sounded to me like you were implying that the behavior of primitive doubles ...
Top answer
1 of 16
183

As others have mentioned, you'll probably want to use the BigDecimal class, if you want to have an exact representation of 11.4.

Now, a little explanation into why this is happening:

The float and double primitive types in Java are floating point numbers, where the number is stored as a binary representation of a fraction and a exponent.

More specifically, a double-precision floating point value such as the double type is a 64-bit value, where:

  • 1 bit denotes the sign (positive or negative).
  • 11 bits for the exponent.
  • 52 bits for the significant digits (the fractional part as a binary).

These parts are combined to produce a double representation of a value.

(Source: Wikipedia: Double precision)

For a detailed description of how floating point values are handled in Java, see the Section 4.2.3: Floating-Point Types, Formats, and Values of the Java Language Specification.

The byte, char, int, long types are fixed-point numbers, which are exact representions of numbers. Unlike fixed point numbers, floating point numbers will some times (safe to assume "most of the time") not be able to return an exact representation of a number. This is the reason why you end up with 11.399999999999 as the result of 5.6 + 5.8.

When requiring a value that is exact, such as 1.5 or 150.1005, you'll want to use one of the fixed-point types, which will be able to represent the number exactly.

As has been mentioned several times already, Java has a BigDecimal class which will handle very large numbers and very small numbers.

From the Java API Reference for the BigDecimal class:

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10^-scale).

There has been many questions on Stack Overflow relating to the matter of floating point numbers and its precision. Here is a list of related questions that may be of interest:

  • Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
  • How to print really big numbers in C++
  • How is floating point stored? When does it matter?
  • Use float or decimal for accounting application dollar amount?

If you really want to get down to the nitty gritty details of floating point numbers, take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

2 of 16
111

When you input a double number, for example, 33.33333333333333, the value you get is actually the closest representable double-precision value, which is exactly:

33.3333333333333285963817615993320941925048828125

Dividing that by 100 gives:

0.333333333333333285963817615993320941925048828125

which also isn't representable as a double-precision number, so again it is rounded to the nearest representable value, which is exactly:

0.3333333333333332593184650249895639717578887939453125

When you print this value out, it gets rounded yet again to 17 decimal digits, giving:

0.33333333333333326
🌐
Coderanch
coderanch.com › t › 529831 › java › Double-toString-return-exact
does Double.toString() always return exact value? [Solved] (Java in General forum at Coderanch)
In the case of 2.3, no other possible double is closer to 2.3 than the one you call d1 is, so "2.3" is sufficient to uniquely identify that value - even if it doesn't look exactly the same. 2) Why does String.format("%.100f", d) return "2.3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"? Format specifiers are described under the API for java...
🌐
CodeGym
codegym.cc › java blog › java numbers › java convert double to string
Java Convert Double to String
September 28, 2023 - Vernier Calliper Double: 7.271572353580126 Vernier Calliper String: 7.271572353580126 · By now, you must be familiar with different ways of converting the Java double to string. But in order to master anything, practise is the key.
Top answer
1 of 4
20

Disclaimer: I only recommend that you use this if speed is an absolute requirement.

On my machine, the following can do 1 million conversions in about 130ms:

 private static final int POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};
 
 public static String format(double val, int precision) {
     StringBuilder sb = new StringBuilder();
     if (val < 0) {
         sb.append('-');
         val = -val;
     }
     int exp = POW10[precision];
     long lval = (long)(val * exp + 0.5);
     sb.append(lval / exp).append('.');
     long fval = lval % exp;
     for (int p = precision - 1; p > 0 && fval < POW10[p]; p--) {
         sb.append('0');
     }
     sb.append(fval);
     return sb.toString();
 }

The code as presented has several shortcomings: it can only handle a limited range of doubles, and it doesn't handle NaNs. The former can be addressed (but only partially) by extending the POW10 array. The latter can be explicitly handled in the code.


If you don't need thread-safe code, you can re-use the buffer for a little more speed (to avoid recreating a new object each time), such as:

  private static final int[] POW10 = {1, 10, 100, 1000, 10000, 100000, 1000000};
  private static final StringBuilder BUFFER = new StringBuilder();

  public String format( double value, final int precision ) {
    final var sb = BUFFER;
    sb.setLength( 0 );

    if( value < 0 ) {
      sb.append( '-' );
      value = -value;
    }

    final int exp = POW10[ precision ];
    final long lval = (long) (value * exp + 0.5);

    sb.append( lval / exp ).append( '.' );

    final long fval = lval % exp;

    for( int p = precision - 1; p > 0 && fval < POW10[ p ]; p-- ) {
      sb.append( '0' );
    }

    sb.append( fval );

    return sb.toString();
  }
2 of 4
8

If you need both speed and precision, I've developed a fast DoubleFormatUtil class at xmlgraphics-commons: http://xmlgraphics.apache.org/commons/changes.html#version_1.5rc1

You can see the code there: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/DoubleFormatUtil.java?view=markup

It's faster than both DecimalFormat/BigDecimal, as fast as Double.toString, it's precise, it's well tested. It's licensed under Apache License 2.0, so you can use it as you want.

🌐
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 - Here is an example: ... We can also use NumberFormat‘s setMaximumFractionDigits() to put constraint on number by decimal places and use its format() method to format 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
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.
🌐
Baeldung
baeldung.com › home › java › java numbers › double precision issue in java
Double Precision Issue in Java | Baeldung
January 8, 2024 - According to the standard, the representation of a double-precision data type consists of three parts: Sign bit – contains the sign of the number (1 bit) Exponent – controls the scale of the number (11 bits) Fraction (Mantissa) – contains the significant digits of the number (52 bits) Now, to understand the double precision issue, let’s perform a simple addition of two decimal numbers:
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
But actually it's more like +1.000000000000000E+001, which can be displayed in String form as "10", "10.0", "10.00", "10.000".... I hope I haven't muddled things; I am saying that if the input was already limited to two digits of precision, and is less than roughly 10^14, your original solution is already working and you just may be misled by what System.out.printf() displays.
Top answer
1 of 5
13

Just using Double.parseDouble() and Double.toString() should work without losing data, I believe. In particular, from the docs for Double.toString():

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.

Another alternative, if you want to preserve the exact string representation (which isn't quite the same thing) is to use BigDecimal in Java.

2 of 5
6

Do you need the string representation for any purpose, or it is merely for a textual data transport (e.g., SOAP/REST message)?

For the latter, you can convert the double value into a long using java.lang.Double.doubleToRawLongBits(double value) and back into a double using java.lang.Double.longBitsToDouble(long value). You can transport the long value as a hex-encoded string.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#doubleToRawLongBits(double) http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#longBitsToDouble(long)

This will preserve the exact 64-bit double value that you have, but it won't be human readable (for most! ;) ).