Katerina Zamani

About Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java String to Double Example

String to Double conversion 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:

Both operations throw NumberFormatException if the string argument is not a parsable number.

2. Example of String to Double conversion

Create a java class with the name StringToDoubleClass and paste the following code.

StringToDoubleClass.java:

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"

Download the source code

This was an example of string to double in Java. Download the source code of this example: StringToDoubleExample.zip

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books