In this article, we will look at different ways to convert a String to a double value in java with example programs and explanation.
[the_ad id=”651″] Method 1: Using parseDouble() method
java.lang.Double class has a parseDouble() method. This method takes a string argument and returns it as a double value.
parseDouble() is a static method which means that we do not need to create an object of Double class to invoke this method. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String numStr = "2.5";
double num = Double.parseDouble(numStr);
System.out.println("Double value is: " + num);
String numStr = "2.5"; double num = Double.parseDouble(numStr); System.out.println("Double value is: " + num);
String numStr = "2.5";
double num = Double.parseDouble(numStr);
System.out.println("Double value is: " + num);

This prints

Double value is: 2.5

String value should be parseable or convertible to double value, otherwise parseDouble() will throw a NumberFormatException.
Thus, below code will throw an exception.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String numStr = "two";
double num = Double.parseDouble(numStr);
String numStr = "two"; double num = Double.parseDouble(numStr);
String numStr = "two"; 
double num = Double.parseDouble(numStr);

Exception in thread “main” java.lang.NumberFormatException: For input string: “two”

Method 2: Using valueOf() method
valueOf() is also a static method in java.lang.Double class which accepts a String argument and returns its double equivalent value. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String numStr = "2.5";
double num = Double.parseDouble(numStr);
System.out.println("Double value is: " + num);
String numStr = "2.5"; double num = Double.parseDouble(numStr); System.out.println("Double value is: " + num);
String numStr = "2.5"; 
double num = Double.parseDouble(numStr); 
System.out.println("Double value is: " + num);

This method also throws a NumberFormatException, if the supplied string is not a parseable number.
[the_ad id=”656″] Method 3: Using Double constructor
java.lang.Double class has a constructor which takes a string argument. This creates an object of Double class.
To get the value in double, use its doubleValue() method. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String numStr = "2.5";
Double d = new Double(numStr);
double num = d.doubleValue();
System.out.println("Double value is: " + num);
String numStr = "2.5"; Double d = new Double(numStr); double num = d.doubleValue(); System.out.println("Double value is: " + num);
String numStr = "2.5"; 
Double d = new Double(numStr);
double num = d.doubleValue();
System.out.println("Double value is: " + num);

This method is deprecated since java 9, its better to use parseDouble() or valueOf() methods.
Java doc states,

It is rarely appropriate to use this constructor. Use parseDouble(String) to convert a string to a double primitive, or use valueOf(String) to convert a string to a Double object.

Method 4: Using DecimalFormat
java.text.DecimalFormat class has a parse() method which takes a String argument and returns an object of type java.lang.Number.
Number is a super class of Double. Its doubleValue() returns a value of type double. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String numStr = "2.5";
try {
Number num = DecimalFormat.getInstance().parse(numStr);
double doubleValue = num.doubleValue();
} catch (ParseException e) {
e.printStackTrace();
}
String numStr = "2.5"; try { Number num = DecimalFormat.getInstance().parse(numStr); double doubleValue = num.doubleValue(); } catch (ParseException e) { e.printStackTrace(); }
String numStr = "2.5";
try {
   Number num = DecimalFormat.getInstance().parse(numStr);
   double doubleValue = num.doubleValue();
} catch (ParseException e) {
   e.printStackTrace();
}

To get an object of DecimalFormat, its static getInstance() method is used.
Note that parse() method of DecimalFormat throws java.text.ParseException, so we need to place it between try-catch block.
In one liner, this can be written as

double doubleValue = DecimalFormat.getInstance().parse(numStr).doubleValue();

Hope the article was helpful and informative.

Categorized in:

Java String,