In this article, we will discuss various ways to convert Double to String in Java
Q) What is the need of converting primitive double or Double wrapper-type to String ?
- Generally, whenever we receive or send data from web application then it is passed in the form of String only
- After mathematical calculation, all numbers need to be converted into String for displaying
- This article explains about primitive double or Double wrapper-type to String conversion only, but we can do conversion for other types like long, float, int, boolean, etc. to String object
- Note: Likewise, sometime String to Double conversion is also required
1. Various ways to convert Double to String
- Using Double.toString(dblValue);
- Using String.valueOf(dblValue);
- Using String.format(type, dblValue);
- Create Double object and then convert to String using toString() method {new Double(dblValue).toString();}
- Adding double quotes (“”) to double value {i.e.; “” + dblValue;}
- Using append() method of StringBuffer & StringBuilder
Read String class in detail with example
Let us move forward and discuss all possible ways to convert Double to String in Java
1.1 Using Double.toString(dblValue); method
- This method can be used to convert primitive double data-type or Double wrapper-type to String object
- Negative double data-type can also be converted to String type, as shown in the below example
Method signature:
public static String toString(double d);
ConvertDoubleIntoStringUsingToStringMethod.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringUsingToStringMethod {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 545.45239438457;
// 1. converting double to String
// by using toString() method
String str1 = Double.toString(dblValue1);
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -54.54538931284743324;
// 2. converting double to String
// by using toString() method
String str2 = Double.toString(dblValue2);
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(123.453478347836);
// 3. converting Double object to String
// by invoking toString() method
String str3 = Double.toString(dblValue3);
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
545.45239438457
2. Converted negative primitive double to String value is :
-54.54538931284743
3. Converted Double object to String value is :
123.453478347836
Final concatenated double strings :
545.45239438457 -54.54538931284743 123.453478347836
1.2 Using String.valueOf(dblValue); method
- This method can be used to convert primitive double data-type or Double wrapper-type to String object
- Negative double data-type can also be converted to String type, as shown in the below example
Method signature:
public static String valueOf(double d);
ConvertDoubleIntoStringUsingValueOfMethod.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringUsingValueOfMethod {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 989.8912921388348347438;
// 1. converting double to String
// by using valueOf() method
String str1 = String.valueOf(dblValue1);
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -98.9894312893478523875;
// 2. converting negative double to String
// by using valueOf() method
String str2 = String.valueOf(dblValue2);
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(123.4532489328373478);
// 3. converting Double object to String
// by using valueOf() method
String str3 = String.valueOf(dblValue3);
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
989.8912921388348
2. Converted negative primitive double to String value is :
-98.98943128934785
3. Converted Double object to String value is :
123.45324893283735
Final concatenated double strings :
989.8912921388348 -98.98943128934785 123.45324893283735
1.3 Using String.format(type, number); method
- This method can be used to convert primitive double data-type or Double wrapper-type to String object
- Negative double data-type can also be converted to String type, as shown in the below example
Method signature:
public static String format(String format, Object... args);
ConvertDoubleIntoStringUsingFormatMethod.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringUsingFormatMethod {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 6.644234878234784578;
// 1. converting double to String
// by using format() method
String str1 = String.format("%f", dblValue1);
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -6.6472f;
// 2. converting negative double to String
// by using format() method
String str2 = String.format("%.2f", dblValue2);
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(54.3212389718324784);
// 3. converting Double object to String
// by using format() method
String str3 = String.format("%.5f", dblValue3);
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
6.644235
2. Converted negative primitive double to String value is :
-6.65
3. Converted Double object to String value is :
54.32124
Final concatenated double strings :
6.644235 -6.65 54.32124
1.4 Create Double object and then convert to String invoking toString() method
- Here, we will create new Double object with primitive double value as constructor-argument
- After creating new Double object by passing primitive double value, then invoke toString() method for converting primitive double data-type to String object
Method signature:
public Double(double value);
public String toString();
ConvertDoubleIntoStringUsingObjectCreation.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringUsingObjectCreation {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 87.54212389843748473;
// 1. converting double to String
// by creating Double object
Double doubleObj1 = new Double(dblValue1);
// and then invoke toString(); method
String str1 = doubleObj1.toString();
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -8754.21490489758785;
// 2. converting negative double to String
// by creating Double object
Double doubleObj2 = new Double(dblValue2);
// and then invoke toString(); method
String str2 = doubleObj2.toString();
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(1.47852349589858);
// 3. converting Double object to String
// by creating Double object
String str3 = dblValue3.toString();
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
87.54212389843748
2. Converted negative primitive double to String value is :
-8754.214904897588
3. Converted Double object to String value is :
1.47852349589858
Final concatenated doubleing strings :
87.54212389843748 -8754.214904897588 1.47852349589858
1.5 Adding double quotes (“”) to double-value
- Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation
Syntax:
String temp = “” + dblValue;
ConvertDoubleIntoStringByAddingDoubleQuotes.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringByAddingDoubleQuotes {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 98.5632348483475;
// 1. converting double to String by adding ""
String str1 = "" + dblValue1;
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -9856.322897384567;
// 2. converting negative double to String by adding ""
String str2 = "" + dblValue2;
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(5.43213419038955784);
// 3. converting Double object to String by adding ""
String str3 = "" + dblValue3;
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
98.5632348483475
2. Converted negative primitive double to String value is :
-9856.322897384567
3. Converted Double object to String value is :
5.432134190389558
Final concatenated double strings :
98.5632348483475 -9856.322897384567 5.432134190389558
1.6 Using append() method of StringBuffer & StringBuilder
- This method can be used to convert primitive double data-type to String object
- Only sign is allowed, like minus (-)
Syntax:
StringBuffer sb = sb.append(dblValue);
String temp = sb.toString();
ConvertDoubleIntoStringByAppending.java
package in.bench.resources.dobule.to.string.conversion;
public class ConvertDoubleIntoStringByAppending {
public static void main(String[] args) {
// primitive double data-type
double dblValue1 = 21.3659324989854;
// 1. converting double to String
// by using append() method
StringBuffer sb1 = new StringBuffer();
String str1 = sb1.append(dblValue1).toString();
System.out.println("1. Converted"
+ " primitive double to String value is : "
+ str1);
// primitive double data-type (negative value)
double dblValue2 = -2136.59349874754545;
// 2. converting negative double to String
// by using append() method
StringBuffer sb2 = new StringBuffer();
String str2 = sb2.append(dblValue2).toString();
System.out.println("\n2. Converted negative"
+ " primitive double to String value is : "
+ str2);
// Double object
Double dblValue3 = new Double(867.514342909358487);
// 3. converting Double object to String
// by using append() method
StringBuffer sb3 = new StringBuffer();
String str3 = sb3.append(dblValue3).toString();
System.out.println("\n3. Converted"
+ " Double object to String value is : "
+ str3);
// converted and concatenated strings using + operator
System.out.println("\nFinal concatenated"
+ " double strings : ");
System.out.println(str1 + " " + str2 + " " + str3);
}
}
Output:
1. Converted primitive double to String value is :
21.3659324989854
2. Converted negative primitive double to String value is :
-2136.5934987475453
3. Converted Double object to String value is :
867.5143429093584
Final concatenated double strings :
21.3659324989854 -2136.5934987475453 867.5143429093584
Q) What if we want to convert to Double wrapper-type to primitive double data-type and vice-versa ?
- Auto-boxing feature available from Java 1.5 version
- So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
- Let’s see one example based on this auto-boxing and un-boxing feature
2. Auto-boxing and un-boxing feature from Java 1.5 version:
- parseDouble() method returns primitive double data-type, but it can be easily used as Double wrapper-type
- as auto-boxing feature helps to convert primitive data-type to wrapper-types
- similarly, Double.valueOf() method returns Double wrapper-type, but it can also be used as primitive double
- Let’s see one example on this auto-boxing feature
AutoBoxingFeatureForDoubleConversion.java
package in.bench.resources.string.to.dobule.conversion;
public class AutoBoxingFeatureForDoubleConversion {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "12.34992832475";
// string to double conversion
double dblValue1 = Double.parseDouble(str1);
// 1. Auto-Boxing - converting double to Double
Double dblAutoBoxing = dblValue1;
System.out.println("1. Auto-Boxing : "
+ dblAutoBoxing);
// String with floating number/digits and minus sign (-)
String str2 = "-98.763429432894328845";
// string to Double conversion
// -98.76342943289433
Double dblValue2 = Double.valueOf(str2);
// 2. Un-Boxing - converting Double to double
double dblUnBoxing = dblValue2;
System.out.println("\n2. Un-Boxing : "
+ dblUnBoxing);
}
}
Output:
1. Auto-Boxing : 12.34992832475
2. Un-Boxing : -98.76342943289433
Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String to int conversion – 3 ways
- Java – Integer to String conversion – 6 ways
- Java – String to float conversion – 3 ways
- Java – Float to String conversion – 6 ways
- Java – String to double conversion – 3 ways
- Java – Double to String conversion – 6 ways
- Java – String to long conversion – 3 ways
- Java – Long to String conversion – 6 ways
- Java – String to boolean conversion – 3 ways
- Java – Boolean to String conversion – 6 ways
- Java – String to char conversion
- Java – Character to String conversion – 6 ways
- Java – String to char[] array conversion – 4 ways
- Java – Character[] array to String conversion – 5 ways
- Java – String to byte conversion – 3 ways
- Java – Byte to String conversion – 5 ways
- Java – String to byte[] array conversion
- Java – Byte[] array to String conversion
- Java – String to short conversion – 3 ways
- Java – Short to String conversion – 5 ways
- Java – StringBuffer to String conversion and vice-versa
- Java – StringBuilder to String conversion and vice-versa
- Java – String to Date conversion
- Java – Date to String conversion
References:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
- https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- https://docs.oracle.com/javase/tutorial/java/data/converting.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
Happy Coding !!
Happy Learning !!