Home » Java Basics » String » Java String to Double Example

About Katerina Zamani

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.


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.


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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Be the First to Comment!

Notify of