There are many ways to convert a double value into a string in Java.
- Double.toString() method
- String.valueOf() method
- Using + operator
- Using StringBuilder or StringBuffer append() function
- Using DecimalFormat
Let’s look at all of the ways one by one
Double.toString() method
Java has a wrapper class Double over the double primitive type and that wrapper class contains one static method toString() which can be used to convert a double into a string.
Method declaration – public static String toString(double d)
What does it do? It will take a double value as an argument and will convert it into a string.
Code example
public class Codekru {
public static void main(String[] args) {
double d = 123.21;
String str = Double.toString(d); // converting double to string
System.out.println("string is: " + str);
}
}
Output –
string is: 123.21
String.valueOf() method
Method declaration – public static String valueOf(double d).
What does it do? – It will also accept a double value into the function’s argument which it will convert into a string.
String class also has one static method ( valueOf() ) which takes a double value into its arguments and converts it into the String. There is an important point to note here that String.valueOf() method internally uses the Double.toString() method and thus the time complexities for conversion will be the same for both of the functions.
Internal implementation of the valueOf() method
public static String valueOf(double d) {
return Double.toString(d);
}
Code example
public class Codekru {
public static void main(String[] args) {
double d = 123.21;
String str = String.valueOf(d); // converting double to a string
System.out.println("string is: " + str);
}
}
Output –
string is: 123.21
Using + operator
This simple operator can also be used to convert a double into a string. Here + operator will be acting as a concatenating operator and will append all of the values at the end of the string as shown below
public class Codekru {
public static void main(String[] args) {
double d1 = 123.21;
double d2 = 145.43;
String str = "" + d1 + d2; // acting as a concatenation operator
System.out.println("string is: " + str);
}
}
Output –
string is: 123.21145.43
Here we can see that both of the double values appended one after the another.
Using StringBuffer or StringBuilder append() function
We can also use the append() function present in the StringBuilder and the StringBuffer classes to convert a double into a string.
Using StringBuffer
public class Codekru {
public static void main(String[] args) {
double d = 13456.346;
String str = new StringBuffer().append(d).toString();
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "13456.346"
}
}
Output –
true
13456.346
Using StringBuilder
public class Codekru {
public static void main(String[] args) {
double d = 13456.346;
String str = new StringBuilder().append(d).toString();
System.out.println(str instanceof String); // will print true
System.out.println(str); // will print "13456.346"
}
}
Output –
true
13456.346
Using DecimalFormat
This method might come in handy when you want to convert a double to a string up to some decimal places or you want to round up to some decimal numbers.
Let’s suppose that we want to round up to two decimal points, then our pattern would be something as shown below
String pattern = "#.##";
This will convert 123.124 to 123.12 and 123.126 to 123.13.
Code example
public class Codekru {
public static void main(String[] args) {
String pattern = "#.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
double d1 = 123.124;
double d2 = 123.126;
String str1 = decimalFormat.format(d1);
String str2 = decimalFormat.format(d2);
System.out.println("string 1 : " + str1);
System.out.println("string 2 : " + str2);
}
}
Output –
string 1 : 123.12
string 2 : 123.13
This is it. If you want to learn about other conversions, then we recommend you to visit this link.
Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at admin@codekru.com.