Java – Typecast or Convert Double to String

You can convert an doubleing point number to string in Java using String class methods.

In this tutorial, we shall go through some of the ways to convert a double to string, with example Java programs.

Double to String using Double.toString() method

toString() is a static method in Double class that returns string object representing the specified doubleing point value. We have to pass the double value as argument to toString() method.

In the following example we convert a number of type double to string by using Double.toString() function.

Java Program

/**
 * 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
ADVERTISEMENT

Double to String using String Concatenation

This is the most easiest way to convert an doubleing point number to a string. All you have to do is add an empty string to it.

The concatenation operator takes double and empty string as arguments and returns a string.

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

Java Program

/**
 * 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

Double to String using String.valueOf() method

We can use String.valueOf() method to get a string value from a doubleing point number. Pass the double as argument to String.valueOf() function, and the function returns a String object.

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

Java Program

/**
 * 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.

Double to String using StringBuffer.append() method

Create a StringBuilder object and append the double value to it using append() method. append() method appends the string representation of the double to this sequence. After append() function, call toString() method on the StringBuilder object, it returns string.

You can create StringBuilder object and call append() function and toString() function as chain in a single statement.

In the following example we shall convert a number of double datatype to string by using StringBuilder class.

Java Program

/**
 * 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

Conclusion

In this Java Tutorial, we learned how to convert or typecast a double to string in Java, using String concatenation or String.valueOf() method.