In this java tutorial, we will learn how to convert double to string in Java. There are several ways we can do this conversion –
1. Java – Convert double to string using String.valueOf(double) method.
2. Convert double to string in Java using toString() method of Double wrapper class.
3. Java – double to string conversion using String.format() method
4. Convert double to string using DecimalFormat.format()
5. Java Convert double to string using StringBuffer and StringBuilder.
1. Java – Convert double to string using String.valueOf(double) method
public static String valueOf(double d)
: We can convert the double primitive to String by calling valueOf() method of String class. This method returns the string representation of the double argument.
public class JavaExample{ public static void main(String args[]){ //double value double dnum = 99.9999; //convert double to string using valueOf() method String str = String.valueOf(dnum); //displaying output string after conversion System.out.println("My String is: "+str); } }
Output:
2. Convert double to string in Java using toString() method of Double wrapper class
public String toString( )
: This is the another method that can be used to convert double to String. This method returns a string representation of the Double object. The primitive double value represented by this object is converted to a string.
public class JavaExample{ public static void main(String args[]){ double dnum = -105.556; //double to string conversion using toString() String str = Double.toString(dnum); System.out.println("My String is: "+str); } }
Output:
3. Java – double to string conversion using String.format() method
String.format() method can be used for the double to string conversion.
public class JavaExample{ public static void main(String args[]){ double dnum = -99.999; String str = String.format("%f", dnum); System.out.println("My String is: "+str); } }
Output:
We can adjust the number of decimal digits in our string using this method. For example: If we want only two digits after decimal point in our string then we can change the code like this:
double dnum = -99.999; String str = String.format("%.2f", dnum);
The output of this code would be: My String is: -100.00
This is because this method round of the double value.
4. Convert double to string using DecimalFormat.format()
Similar to String.format() method. To use this, we have to import the package: java.text.DecimalFormat in our code.
import java.text.DecimalFormat; public class JavaExample{ public static void main(String args[]){ double dnum = -99.999; /* creating instance of DecimalFormat * #.000 is to have 3 digits after decimal point * in our output string */ DecimalFormat df = new DecimalFormat("#.000"); //conversion String str = df.format(dnum); //displaying output System.out.println("My String is: "+str); } }
Output:
5. Java Convert double to string using StringBuffer and StringBuilder
We can convert double to string using StringBuffer and StringBuilder as well. The steps of conversion are same for both. The steps are as follows –
1. Create StringBuffer/StringBuilder instance
2. Append double value
3. Convert StringBuffer/StringBuilder to String
double ->StringBuffer ->String
public class JavaExample{ public static void main(String args[]){ //double value double dnum = 89.891; //creating instance of StringBuffer StringBuffer sb = new StringBuffer(); //appending the double value to StringBuffer instance sb.append(dnum); //converting StringBuffer to String String str = sb.toString(); System.out.println("My String is: "+str); } }
Output:
My String is: 89.891
double ->StringBuilder ->String
public class JavaExample{ public static void main(String args[]){ //double value double dnum = -66.89; //creating instance of StringBuilder StringBuilder sb = new StringBuilder(); //appending the double value to StringBuilder instance sb.append(dnum); //converting StringBuilder to String String str = sb.toString(); System.out.println("My String is: "+str); } }
Output:
Claus says
Here is another one:
Double var = 33.33;
String str = “” + var;
Or is there any drawback using this?
And it works with every proimitive data type.