This blog will demonstrate the methods for converting double to String in Java.
How to Convert double to String in Java?
For double to String conversion in Java, you can use:
- Double.toString() method
- String.valueOf() method
- “+” operator
- String.format() method
- StringBuilder.append() method
- StringBuffer.append() method
We will discuss each of the mentioned methods one by one!
Method 1: Convert double to String in Java Using Double.toString() Method
To convert a double to String, you can use the “toString()” static method of the Double class. It converts double numeric values to String. As it is a static method, we do not need to create an object and call the method with the class name.
Syntax
Here, the “toString()” method will convert the “val” double variable to String.
Example
Firstly, we will create a variable of double data type named “val” and assign the following value:
Next, we will call the “Double.toString()” method and pass the created double variable as an argument to it:
The below print statement will return “true” if the converted variable that is “str” is a String instance; otherwise, it will return “false”:
The output displays “true”, which indicates that the data type of the resultant value is String:
Method 2: Convert double to String in Java Using String.valueOf() Method
You can also utilize the “valueOf()” String class static method for double to String conversion. It takes the decimal value of any type, such as double, as a parameter and converts it to a String.
Syntax
Here, the “valueOf()” method will convert the “val” double variable to String.
Example
Firstly, we will create a double type variable named “val” and store the following value in it:
Next, we will call the “String.valueOf()” method by passing the “val” as a parameter:
Lastly, we will check if the converted value is a String instance or not:
Output
Method 3: Convert double to String in Java Using “+” Operator
The simplest way to convert double to String is using the “+” Addition operator. It acts as a concatenation operator when utilized with Strings. Similarly, a double value can be converted to a String by simply concatenating it with an empty string.
Syntax
Here, the “+” operator will concatenate the “val” double type variable with an empty string, which results in its double to String conversion.
Example
First, we will store a decimal value in a variable “val”:
Then, create a String type variable “str” that stores the converted string after concatenating “val” with an empty string:
Lastly, we will check if the converted value is a String instance or not:
Output
Now, head towards the next section!
Method 4: Convert double to String in Java Using String.format() Method
The “String.format()” method can also be utilized for double to String conversion. In this method, we will pass the decimal value with the “%f” specifier, which indicates that the second parameter contains floating-point numbers. It then converts the double value to the String format.
Syntax
Here, the “String.format()” method will convert the “val” double type variable to the String format.
Example
In this example, we have a double variable “val” with the following value:
We will now call the “String.format()” method and pass “%f” as a specifier and “val” as the second parameter:
Finally, we will check if the converted value is a String instance or not:
Output
Method 5: Convert double to String in Java Using StringBuilder.append() Method
In Java, the “append()” method of the “StringBuilder” class is also used for double to String conversion.
Syntax
To utilize this method, firstly, we will create an object of the StringBuilder class, then append the value of the double variable “val” in it and convert it to String.
Example
Now, we will create a new object of the “StringBuilder” class named “str” and call the “append()” method by passing the “val” double variable and converting it to the string with the “toString()” method:
Output
Method 6: Convert double to String in Java Using StringBuffer.append() Method
There is another method to convert a double to a String, which is the “append()” method of the “StringBuffer” class. It works the same as the above “StringBuilder.append()” method. It also creates an object of the StringBuffer class to access the methods and then convert to String.
Syntax
To utilize this method, firstly, we will create an object of the StringBuffer class, then append the value of the double variable “val” in it and convert it to String.
Example
Firstly, create an instance of the StringBuffer class and then call the append() method by passing a double value which will be converted into a String by using the “toString()” method:
Output
We have offered the basic information related to double to String conversion in Java.
Conclusion
To convert double to String in Java, you can utilize different methods such as Double.toString(), String.valueOf(), the “+” operator, String.format(), StringBuilder.append(), and StringBuffer.append() method. The first four methods do not need any additional object creation for the specified purpose, whereas, for the last two methods, it is required to create an object of the particular class and then call its related method. In this blog, we have demonstrated the methods for double to String conversion.