Grails Cookbook - A collection of tutorials and examples

Conversion Of Java String To Double

Converting Java String To Double is one of the important topic in creating convenient systems in the world of Java. This tutorial will help you find different ways to come up with our desired value.


Conversion Using Double.parseDouble()

The static method Double.parseDouble() returns a double output and it parses the string argument.

Take Note Of The Syntax

public static double parseDouble(String s) throws NumberFormatException
Understand that the parameter s shall be changed into a primitive Double value. If the parameter is an invalid double, expect the NumberFormatException.

Understanding The Example

String numAsString = "100.53";
double theNumber = Double.parseDouble(numAsString);
System.out.println("The result is: " + theNumber);
Check out the result below:
The result is: 100.53
This is one of many solution in converting a String to Java Double, one thing to remember is that the output value is just a primitive double value and that it is not an instance of the Double class.


Conversion Using Double.valueOf()

Double.valueOf() is a static method that returns a Double object which holds the value of the stated String.

Take Note Of The Syntax

public static Double valueOf(String s) throws NumberFormatException
If the parameter is an invalid double, Java will throw a NumberFormatException.
String numAsString = "100.53";
double theNumber = Double.valueOf(numAsString);
System.out.println("The result is: " + theNumber);
Check out the result below:
The result is: 100.53
This is the most common method when you wish to convert a String to Double. Note that the resulting value is an instance of the Double class and not a primitive double value.


Conversion Using new Double(String).doubleValue()

Our second option would have an invoking process - creating an instance of Double class and invoking it's doubleValue() method.

Understanding The Example

String numAsString = "100.53";
Double doubleObject = new Double(numAsString);
double theNumber = doubleObject.doubleValue();
This is the shortcut version:
String numAsString = "100.53";
double theNumber = new Double(numAsString).doubleValue();

Or might as well write this:

double theNumber = new Double("100.53").doubleValue();


Conversion Using DecimalFormat

To change a number to it's String representation, we can use the java.text.DecimalFormat class. Know that this class can also be utilized the other way around which parses into it's numerical representation.

Understanding The Example

String numAsString = "100.53";
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
   double theNumber = decimalFormat.parse(numAsString).doubleValue();
   System.out.println("The result is: " + theNumber);
} catch (ParseException e) {
   System.out.println(numAsString + " is an invalid number.");
}
Check out the result below:
The result is: 100.53
Understand that parse() method will remind us for a ParseException should we encounter a problem with the String.