Java

How to Convert double to String in Java

In Java, “double” is an object of the Java “Double” wrapper class which stores the primitive data type double as a parameter. However, sometimes, we need to convert double to String because Strings allow us to store larger numbers that do not fit in any data types that hold the numbers, such as integers or doubles.

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

Double.toString(val);

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:

double val= 783.8956d;

Next, we will call the “Double.toString()” method and pass the created double variable as an argument to it:

String str= Double.toString(val);

The below print statement will return “true” if the converted variable that is “str” is a String instance; otherwise, it will return “false”:

System.out.print(str instanceof String);

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

String.valueOf(val)

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:

double val= 583.856d;

Next, we will call the “String.valueOf()” method by passing the “val” as a parameter:

String str= String.valueOf(val);

Lastly, we will check if the converted value is a String instance or not:

System.out.print(str instanceof String);

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

val + ""

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”:

double val= 543.656d;

Then, create a String type variable “str” that stores the converted string after concatenating “val” with an empty string:

String str=  val + "" ;

Lastly, we will check if the converted value is a String instance or not:

System.out.print(str instanceof String);

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

String.format("%f", val)

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:

double val= 1233.676d;

We will now call the “String.format()” method and pass “%f” as a specifier and “val” as the second parameter:

String str = String.format("%f", val);

Finally, we will check if the converted value is a String instance or not:

System.out.print(str instanceof String);

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

String str = new StringBuilder().append(val).toString();

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:

String str = new StringBuilder().append(val).toString();

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

String str = new StringBuffer().append(val).toString();

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:

String str = new StringBuffer().append(val).toString();

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.