How to convert double to String in Java

How to Convert double to String in Java

It is used when we want to convert double primitive to String type. There are two methods to convert double to String.

  • Using String.valueOf() method
  • Using Double.toString() method

Using String.valueOf() method

You can convert double primitive to String by calling the valueOf() method of String class. String.valueOf() is an overloaded method. It returns the String representation of the double argument. The signature of the method is given below:

Example

In this example, we have a variable sum of type double that contains value 34.22. str is a variable of type String that stores the converted value of sum.  The String is a class. The valueOf () is a method of String class which parses sum as an argument. The println statement prints the converted value of the variable sum.

Output

Using Double.toString() method

It also converts double to String. It is the static method of Double class. The signature of the method is given below:

Example

In this example, we have a variable pi of type double that contains value 3.141. val is a variable of type String that stores the converted value of pi. The Double is a wrapper class. The toString() is a method of Double wrapper class parses an argument pi of double type. The println statement prints the converted value of the variable pi.

Output