How to convert double to string in Java

In this post, we are going to convert double to String in Java. double is one of the Java data types that is used to store a wide range of floating-point values while String is a sequence of characters.

To convert double type to string, we can use the valueOf() method of String class or toString() method of the Double class. Double is a wrapper class in Java that is used to handle double type objects.

The valueOf() method of String class is used to get a string from double. It takes a single argument and returns a string of the specified type.

The toString() method of the Double class returns a string of double type value.

We used plus (+) operator as well to convert double to string because this operator used to concatenate two objects and returns a string.

Time for an Example:

Let's take an example to convert double to a string. Here, we are using the valueOf() method of String class that returns a string.

public class Main {
	public static void main(String[] args){
		double val = 21.50;
		System.out.println(val);
		// double to String
		String str = String.valueOf(val);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


21.5
21.5
java.lang.String

Example:

Let's take another example to get a string from a double type. Here, we are using the toString() method of the Double class that returns a string by converting double.

public class Main {
	public static void main(String[] args){
		double val = 21.50;
		System.out.println(val);
		// double to String
		String str = Double.toString(val);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


21.5
21.5
java.lang.String

Example:

In this example, we are using plus(+) operator to concatenate double value with a string value. The plus operator returns a string value after concatenation.

public class Main {
	public static void main(String[] args){
		double val = 21.50;
		System.out.println(val);
		// double to String
		String str = ""+val;
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


21.5
21.5
java.lang.String