Table of Contents
Convert double to String in Java
In java, double is a primitive data type whereas Double is a wrapper class. As we know that java supports auto-boxing, so we can use double and Double interchangeably in most of the cases.
In several applications, we may need to convert a number to a string because we need to operate on the value in its string form. In many GUI based application also, if we have to display double value in text field we may need to convert. There are several ways to convert double type data to a string.
The list below shows eight different ways in which we can convert double to string.
- Using
+
operator - Using
String.valueOf()
- Using
Double.toString()
- Using
DecimalFormat
class - Using
String.format()
- Using
StringBuilder
class - Using
StringBuffer
class - Using Double constructor
Using + operator
This is the simplest approach for converting double to a string. Here, we are using a concatenation operator + to store the double value as a string.
Example : In this example, we concatenate blank string "" with double variable d.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
String s = d + "";
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.4854
Using String.valueOf()
In this approach, we are using valueOf
method of String class that converts values passed as a parameter into a string.
Example : In this example, double variable d is converted to string using valueOf
method.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
String s = String.valueOf(d);
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.4854
Using Double.toString()
In this approach, we are using Double class to convert double value to a string. Each of the Number subclasses includes a class method, toString()
, that will convert its primitive type to a string.
Example :In this example, we are using toString
method of Double class for conversion.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
String s = Double.toString(d);
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.4854
Using DecimalFormat class
In this approach, we are using the DecimalFormat
class to convert double to String. Moreover, we can also get string representation with specified decimal places and rounding up the double number.
Example :In this example, we are using format method of DecimalFormat
class for conversion. Here, we are also rounding up a double number to two digits and then converting to string.
import java.text.DecimalFormat;
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.48549;
// Converting double to string
DecimalFormat df = new DecimalFormat();
String s1 = df.format(d);
df = new DecimalFormat("#.0#");
String s2 = df.format(d);
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable s1 is " + s1);
System.out.println("The value of String variable s2 is " + s2);
}
}
Output
The value of double variable is 858.4854
The value of String variable s1 is 858.485
The value of String variable s2 is 858.49
Using String.format()
In this approach, we are using format method of a String class to convert double to string.
Example : In this example, we will use the access specifier "%f
" as the value to format is a double type.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
String s = String.format("%f", d);
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.485400
Using StringBuilder class
In this approach, we are using append method of StringBuilder
class. The append()
method is used to append the string representation of the double argument to this sequence.
Example : In this example, we will use the append()
method to add double value and toString()
method to convert it to string.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
StringBuilder b = new StringBuilder();
String s = b.append(d).toString();
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.4854
Using StringBuffer class
In this approach, we are using append method of StringBuffer
class. The append()
method is used to append the string representation of the double argument to this sequence.
Example : In this example, we will use the append()
method to add double value and toString()
method to convert it to string.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
StringBuffer b = new StringBuffer();
String s = b.append(d).toString();
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
The value of double variable is 858.4854
The value of String variable is 858.4854
Using Double Constructor
In this approach, we are using constructor of double class to initialize with double value and then use toString()
method to convert double object to the string. Although, this approach is deprecated from Java version 9, it can be used in the older versions.
Example : In this example, we will use the access specifier "%f
" as the value to format is a double type.
// Program to convert double to string in Java
public class Main {
public static void main(String[] args) {
// Initializing the variable
double d = 858.4854;
// Converting double to string
Double o = new Double(d);
String s = o.toString();
// Printing both the variables
System.out.println("The value of double variable is " + d);
System.out.println("The value of String variable is " + s);
}
}
Output
Note: Main.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. The value of double variable is 858.4854 The value of String variable is 858.4854
Summary
The knowledge of converting double value to a String in Java is very useful while working on real time applications. In this tutorial, we covered eight different approaches to convert double to String in Java. As per the requirement of an application, we can choose an appropriate approach for conversion. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on conversion of double value to a string object in Java.
References
Converting between Numbers and Strings
DecimalFormat Class
Strings Class
StringBuilder Class
StringBuffer Class