Given a Double value in Java, the task is to convert this double value to string type.
Examples:
Input: 1.0
Output: "1.0"
Input: 3.14
Output: "3.14"
Approach 1: (Using + operator)
One method is to create a string variable and then append the double value to the string variable. This will directly convert the double value to string and add it in the string variable.
Below is the implementation of the above approach:
Example 1:
Output:
1.0 after converting into string = 1.0
Approach 2: (Using String.valueOf() method)
The simplest way to do so is using valueOf() method of
String class in
java.lang package. This method takes the double value to be parsed and returns the value in String type from it.
Syntax:
String.valueOf(doubleValue);
Below is the implementation of the above approach:
Example 1: