String to Double Java Example
In this article, we will show you how to convert string to double in java. String to Double conversion in Java is a frequent procedure because both data types are commonly used.
As you can imagine in this example we are going to show all the possible ways of converting String
object to Double
object.
1. Syntax of the convert operations
First of all, in order to convert a string to double, we can simply create a Double
by setting the specified string to its constructor. Otherwise there are two different operations:
public static Double valueOf(String s)
: it is a static method that returns aDouble
object, which keeps the double value of the specifiedString
public static double parseDouble(String s)
: this is also a static method that returns a new double, represented by theString
we specified.
Both operations throw NumberFormatException
if the string argument is not a parsable number.
2. Example of String to Double conversion in Java
This is an example of how to convert string to double in java. Create a java class with the name StringToDoubleClass
and paste the following code.
StringToDoubleClass.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.javacodegeeks.core.stringtodouble; public class StringToDoubleClass { public static void main(String[] args) { String doubleTest = "10.0" ; String doubleTest2 = "-0010.0000" ; Double d1 = new Double(doubleTest); System.out.println( "Use of Double constructor: " + d1); try { // use of valueOf with both test strings Double d2 = Double.valueOf(doubleTest); System.out.println( "Use of valueOf with doubleTest: " + d2); Double d2test = Double.valueOf(doubleTest2); System.out.println( "Use of valueOf with doubleTest2: " + d2test); // use of parseDouble with both test strings double d3 = Double.parseDouble(doubleTest); System.out.println( "Use of parseDouble with doubleTest: " + d3); double d3test = Double.parseDouble(doubleTest2); System.out.println( "Use of parseDouble with doubleTest2: " + d3test); // give an int-string double dInt = Double.parseDouble( "12" ); System.out.println( "Int to parseDouble: " + dInt); // error condition Double dError = Double.valueOf( "jcg" ); System.out.println( "Error to valueOf: " + dError); } catch (NumberFormatException e) { System.err.println( "NumberFormatException in valueOf, " + e.getMessage()); } } } |
Now lets explain the code above a little bit. Notice that the value of doubleTest2
is a negative double number with two leading zeros and some extra zeros in the decimal part. As you can see in the output below, both valueOf()
and parseDouble()
methods return -10.0 number. Also in the situation of the parameter dInt
, if the specified string represents an integer, the two operations convert it to double again. Of course, if the string can not be parsed to a number, NumberFormatException
is thrown.
Below are the results of the code execution.
Output
Use of Double constructor: 10.0 Use of valueOf with doubleTest: 10.0 Use of valueOf with doubleTest2: -10.0 Use of parseDouble with doubleTest: 10.0 Use of parseDouble with doubleTest2: -10.0 Int to parseDouble: 12.0 NumberFormatException in valueOf, For input string: "jcg"
3. Download the source code
This was an example of string to double in Java.
Download the source code of this example: String to Double Java Example
Last updated on Apr. 22nd, 2020