Java – Convert Double to String

To convert a double value to a String in Java, you can use Double.toString(double) or String.valueOf(double). Both methods return a string representation of the numeric value.

A double cannot be typecast directly to String because they are different kinds of types. The value must be converted. If you need a specific display format, such as exactly two decimal places or a value without scientific notation, use a formatting approach instead of a basic conversion method.

Java double to String conversion syntax

</>
Copy
String str1 = Double.toString(doubleValue);
String str2 = String.valueOf(doubleValue);

For example, when doubleValue is 12.75, both statements produce the string "12.75".

Convert double to String using Double.toString()

Double.toString() is a static method of the Double class. Pass a double value as the argument, and the method returns its string representation.

In the following example we convert numeric values to strings using Double.toString().

Java Program

</>
Copy
/**
 * Java Program - Convert Double to String
 */

public class DoubleToString {

	public static void main(String[] args) {
		String str = Double.toString(1.63869f);
		System.out.println(str);
		
		str = Double.toString(-25.963869f);
		System.out.println(str);
	}
}

Output

1.63869
-25.96387

The literals in the existing example use the f suffix, so they are created as float values and then widened to double when passed to Double.toString(). For a double literal, normally omit the suffix or use d, as in 25.963869 or 25.963869d.

Convert double to String using String.valueOf()

String.valueOf(double) is another direct way to convert a primitive double to String. Pass the number to the method, and it returns the corresponding string representation.

In the following example we shall convert a number of double datatype to string by using String.valueOf() function.

Java Program

</>
Copy
/**
 * Java Program - Convert Double to String
 */

public class DoubleToString {

	public static void main(String[] args) {
		double n = 52.4839f;
		
		//convert double to string using String methods
		String str = String.valueOf(n);
		System.out.print(str);
	}
}

Double value has been converted to String.

Output

52.4838981628418

The longer value in this output is caused by the original program’s f suffix. The literal is first stored with float precision and is then widened to double. With a regular double literal such as 52.4839, String.valueOf() produces "52.4839".

Convert double to String using String Concatenation

A double can also be converted to a string by concatenating it with an empty string. Because one operand of + is a String, Java performs string concatenation and converts the numeric value to text.

In the following example we shall convert a double to string by concatenating the number to an empty string.

Java Program

</>
Copy
/**
 * Java Program - Convert Double to String
 */

public class DoubleToString {

	public static void main(String[] args) {
		double n = 52.52639f;
		
		//convert double to string using string concatenation
		String str = n + "";
		System.out.print(str);
	}
}

Output

52.52639

Concatenation works, but Double.toString() or String.valueOf() expresses the conversion more clearly when you only need the numeric value as a string.

Convert double to String using StringBuffer.append()

StringBuffer.append(double) appends the string representation of a double value to the buffer. Calling toString() on the buffer then returns the complete contents as a String.

This approach is useful when a StringBuffer is already being used to assemble a larger string. For converting one number by itself, a direct conversion method is simpler.

In the following example, append() and toString() are chained in a single statement.

Java Program

</>
Copy
/**
 * Java Program - Convert Double to String
 */

public class DoubleToString {

	public static void main(String[] args) {
		String str = (new StringBuffer()).append(1.63869f).toString();
		System.out.println(str);
		
		str = (new StringBuffer()).append(-25.963869f).toString();
		System.out.println(str);
	}
}

Output

1.63869
-25.96387

Convert double to String with two decimal places in Java

Basic conversion methods preserve a suitable textual representation of the numeric value; they do not guarantee a fixed number of digits after the decimal point. When the string is intended for display and must contain exactly two decimal places, format the number instead.

String.format() can format a double to two decimal places. Using Locale.ROOT makes the decimal separator predictable as a period regardless of the system’s default locale.

</>
Copy
import java.util.Locale;

public class DoubleToStringTwoDecimals {
    public static void main(String[] args) {
        double number = 52.52639;

        String str = String.format(Locale.ROOT, "%.2f", number);
        System.out.println(str);
    }
}

Output

52.53

The format specifier %.2f requests fixed-point notation with two digits after the decimal point. The value is rounded as part of formatting.

Convert double to String without scientific notation

Double.toString() may use scientific notation for values where that representation is appropriate. If you specifically need ordinary decimal notation, one option is to create a BigDecimal with BigDecimal.valueOf(double) and call toPlainString().

</>
Copy
import java.math.BigDecimal;

public class DoubleWithoutScientificNotation {
    public static void main(String[] args) {
        double number = 1.25E20;

        String str = BigDecimal.valueOf(number).toPlainString();
        System.out.println(str);
    }
}

Output

125000000000000000000

This approach controls the textual notation; it does not add precision that was not present in the original double.

Double.toString() vs String.valueOf() for double conversion

ApproachWhat it doesSuitable use
Double.toString(value)Converts a primitive double to its string representationDirect, explicit double-to-String conversion
String.valueOf(value)Converts the supplied double to a stringGeneral-purpose String conversion
value + ""Uses string concatenationWorks, but is less explicit for conversion-only code
String.format(...)Formats the value according to a patternFixed decimal places and display-oriented output
BigDecimal.valueOf(value).toPlainString()Produces a plain decimal representationAvoiding scientific notation for finite double values

For ordinary conversion, Double.toString() and String.valueOf() are the most direct choices. Use formatting methods only when the required string has presentation rules such as a fixed number of decimal places.

Why a converted double may show unexpected decimal digits

Java’s double type uses binary floating-point representation. Many decimal fractions cannot be represented exactly as binary floating-point values. As a result, calculations can produce values with decimal digits that differ slightly from the decimal numbers originally written in the source code.

</>
Copy
public class DoublePrecisionExample {
    public static void main(String[] args) {
        double value = 0.1 + 0.2;
        String str = Double.toString(value);

        System.out.println(str);
    }
}

Output

0.30000000000000004

The conversion method is not introducing this difference. It is converting the double value that already resulted from the floating-point calculation. If the string is intended for display, format it to the required number of decimal places.

Choosing the right Java double-to-String method

Use Double.toString(value) when you want an explicit double-specific conversion, or String.valueOf(value) for a concise general conversion. Use String.format() when you need fixed decimal places, and use a plain-decimal approach such as BigDecimal.valueOf(value).toPlainString() when scientific notation is not acceptable.

In this Java Tutorial, we learned how to convert a double to string in Java using Double.toString(), String.valueOf(), string concatenation, and StringBuffer.append(), and how to format a double when the resulting string needs two decimal places or plain decimal notation.