In this article, we will discuss various ways to convert String to Double in Java
Q) What is the need of converting String to primitive double or Double wrapper-type ?
- Generally, whenever we receive any data from web application then it is passed in the form of String only
- To perform any mathematical operations on numbers which are received as String, need to be converted into respective type for further calculation
- This article explains about String to primitive double or Double wrapper-type conversion only, but we can do conversion for other types like long, float, int, boolean, etc.
- Note: Likewise, sometimes Double to String conversion is also required
1. Various ways to convert String to double (or Double)
- Using Double.parseDouble(“strValue”);
- Using Double.valueOf(“strValue”);
- Create Double object and then invoke doubleValue() method {i.e.; new Double(“strValue”).doubleValue();}
Read String class in detail with example
Let us move forward and discuss all possible ways to convert String to Double in Java
1.1 Using Double.parseDouble(“strValue”); method
- This method can be used to convert String which constitutes of floating numbers (or digits) into primitive double data-type
- Only sign is allowed, like minus (-)
- Note: This conversion is done for higher precision values when compared with float value which is for lower precision
- If passed string argument isn’t correctly formatted then NumberFormatException will be thrown as shown in the below E.1 example
Method signature:
public static double parseDouble(String s) throws NumberFormatException;
ConvertStringIntoDoubleUsingParseFloatMethod.java
package in.bench.resources.string.to.dobule.conversion;
public class ConvertStringIntoDoubleUsingParseFloatMethod {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "123.459876543123412";
// 1. converting String to float
double dblValue1 = Double.parseDouble(str1);
System.out.println("1. Converted double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-123.498765431231522";
// 2. converting String to float
double dblValue2 = Double.parseDouble(str2);
System.out.println("\n2. Converted double value is : "
+ dblValue2);
}
}
Output:
1. Converted double value is : 123.45987654312341
2. Converted double value is : -123.49876543123152
1.2 Using Double.valueOf(“strValue”); method
- This method can be used to convert String which constitutes of floating numbers (or digits) into Double wrapper-type
- Only sign is allowed, like minus (-)
- Note: This conversion is done for higher precision values when compared with float value
- If passed string isn’t correctly formatted then NumberFormatException will be thrown as shown in the below E.2 example
Method signature:
public static Double valueOf(String s) throws NumberFormatException;
ConvertStringIntoDoubleUsingValueOfMethod.java
package in.bench.resources.string.to.dobule.conversion;
public class ConvertStringIntoDoubleUsingValueOfMethod {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "98.769876543123415";
// 1. converting String to Double
Double dblValue1 = Double.valueOf(str1);
System.out.println("1. Converted Double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-98.798765431234165";
// 2. converting String to Double
Double dblValue2 = Double.valueOf(str2);
System.out.println("\n2. Converted Double value is : "
+ dblValue2);
}
}
Output:
1. Converted Double value is : 98.76987654312342
2. Converted Double value is : -98.79876543123416
1.3 Create Double object and then invoke doubleValue() method
- Here, we will create new Double object with String as constructor-argument
- After creating new Double object by passing string value, then invoke doubleValue() method for converting String to primitive double data-type
- Passed string to constructor-argument should constitutes of floating numbers (or digits)
- Only sign is allowed, like minus (-)
- Note: This conversion is done for higher precision values when compared with float value
- If passed string isn’t correctly formatted then NumberFormatException will be thrown as shown in the below E.3 example
Method signature:
public Double(String s) throws NumberFormatException;
public double doubleValue();
ConvertStringIntoFDoubleUsingDoubleValueMethod.java
package in.bench.resources.string.to.dobule.conversion;
public class ConvertStringIntoFDoubleUsingDoubleValueMethod {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "5.69876543123414545";
// 1. converting String to double
// by creating new Double Object
Double double1 = new Double(str1);
double dblValue1 = double1.doubleValue();
System.out.println("1. Converted double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-5.45456987654312341";
// 2. converting String to double
// by creating new Double Object
Double double2 = new Double(str2);
double dblValue2 = double2.doubleValue();
System.out.println("\n2. Converted double value is : "
+ dblValue2);
}
}
Output:
1. Converted double value is : 5.698765431234145
2. Converted double value is : -5.454569876543124
Exception scenario:
- handle exception properly in all 3 ways
- otherwise NumberFormatException will be thrown for incorrect string values (or not correctly formatted string)
- and terminates program abruptly
- we will see 3 different examples for all cases with E.1, E.2 and E.3
2. Exception scenarios:
- In all above 3 cases, whenever string isn’t properly formatted to convert String to primitve double or Double wrapper-type, then NumberFormatException will be thrown
- We will see, what happens if string isn’t correctly formatted for all 3 ways of conversion
2.1 Handle NumberFormatException while converting String to double using Double.parseDouble() method
ParseDoubleMethodThrowsNFE.java
package in.bench.resources.string.to.dobule.conversion;
public class ParseDoubleMethodThrowsNFE {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "12.3ben";
// 1. converting String to double
double dblValue1 = Double.parseDouble(str1);
System.out.println("1. Converted double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-12.3ben";
// 2. converting String to double
double dblValue2 = Double.parseDouble(str2);
System.out.println("\n2. Converted double value is : "
+ dblValue2);
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException:
For input string: "12.3ben"
at sun.misc.FloatingDecimal.readJavaFormatString(
FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(
FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at in.bench.resources.string.to.dobule.conversion
.ParseDoubleMethodThrowsNFE
.main(ParseDoubleMethodThrowsNFE.java:11)
2.2 Handle NumberFormatException while converting String to double using Double.valueOf() method
DoubleValueOfMethodThrowsNFE.java
package in.bench.resources.string.to.dobule.conversion;
public class DoubleValueOfMethodThrowsNFE {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "54.5345678ben";
// 1. converting String to double
// by creating new Double Object
Double double1 = new Double(str1);
double dblValue1 = double1.doubleValue();
System.out.println("1. Converted double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-54.5456758679898ben";
// 2. converting String to double
// by creating new Double Object
Double double2 = new Double(str2);
double dblValue2 = double2.doubleValue();
System.out.println("\n2. Converted double value is : "
+ dblValue2);
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException:
For input string: "54.5345678ben"
at sun.misc.FloatingDecimal.readJavaFormatString(
FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(
FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.<init>(Double.java:608)
at in.bench.resources.string.to.dobule.conversion
.DoubleValueOfMethodThrowsNFE
.main(DoubleValueOfMethodThrowsNFE.java:11)
2.3 Handle NumberFormatException while converting String to float using doubleValue() method
DoubleValueMethodThrowsNFE.java
package in.bench.resources.string.to.dobule.conversion;
public class DoubleValueMethodThrowsNFE {
public static void main(String[] args) {
// String with floating numbers or digits
String str1 = "98.734t54356667ben1";
// 1. converting String to double
// by creating new Double Object
Double double1 = new Double(str1);
double dblValue1 = double1.doubleValue();
System.out.println("1. Converted double value is : "
+ dblValue1);
// String with floating number/digits and minus sign (-)
String str2 = "-98.7123324658978ben";
// 2. converting String to double
// by creating new Double Object
Double double2 = new Double(str2);
double dblValue2 = double2.doubleValue();
System.out.println("\n2. Converted double value is : "
+ dblValue2);
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException:
For input string: "98.734t54356667ben1"
at sun.misc.FloatingDecimal.readJavaFormatString(
FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(
FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.<init>(Double.java:608)
at in.bench.resources.string.to.dobule.conversion
.DoubleValueMethodThrowsNFE
.main(DoubleValueMethodThrowsNFE.java:11)
Q) What if we want to convert primitive double data-type to Double wrapper-type or 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 feature
3. Auto-boxing and un-boxing feature from Java 1.5 version:
- parseDouble() method returns primitive double type but it can be easily used as Double wrapper-type
- as auto-boxing feature helps to convert primitive type to wrapper-types
- similarly, Double.valueOf() method returns Double wrapper-type but it can also be used as primitive double data-type
- let’s see one example on this auto-boxing feature at the very end
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/Double.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 !!