Pages

Friday, November 5, 2021

Java Convert String to Double Examples

1. Overview

In this article, We'll learn how to convert the String values into double values in java using various methods and what are the problem when the string is with the valid double value.

First, We'll explore the examples on the following methods in order.

  • Double.parseDouble(String)
  • Double.valueOf(String)
  • Double Constructor
  • With invalid double string values
Java Convert String to Double Examples


2. Using Double.parseDouble()


First, let us look at the simple method which is Double.parseDouble(). Double class has many static utility methods and parseDouble() is one of them.


parseDouble() takes the String argument as input and returns double value.
package com.javaprogramto.programs.strings.todouble;

public class StringToDoubleExample1 {

	public static void main(String[] args) {

		// double value in string format
		String to_double = "1234";

		double doubleValue = Double.parseDouble(to_double);

		System.out.println("String to Double value : " + doubleValue);

		to_double = "100d";

		doubleValue = Double.parseDouble(to_double);

		System.out.println("String to Double value with d in the string : " + doubleValue);

	}
}

Output:
String to Double value : 1234.0
String to Double value with d in the string : 100.0

In the double string only d character is allowed to represent the double. Other than d are not allowed.

3. Using Double.valueOf()


Double class another utility method valueOf() which also takes the String as argument and returns the double value. But the difference between Double parseDouble() and valueOf() is parseDouble() returns primitive double, other valueOf() returns Double wrapper instance.

valueOf() internally calls the parseDouble() method.

The below example generates the same output as above.
package com.javaprogramto.programs.strings.todouble;

public class StringToDoubleExample2 {

	public static void main(String[] args) {

		// double value in string format
		String to_double = "1234";

		Double doubleValue = Double.valueOf(to_double);

		System.out.println("String to Double value with valueOf() : " + doubleValue);

		to_double = "100d";

		doubleValue = Double.valueOf(to_double);

		System.out.println("String to Double value with d in the string using valueOf() : " + doubleValue);

	}
}

4. Using Double Constructor


String can be converted to double directly using the Double class constructor as below. But this way is deprecated. Use parseDouble() for primitive and valueOf() for Double object instead of Double constructor.
package com.javaprogramto.programs.strings.todouble;

public class StringToDoubleExample3 {

	public static void main(String[] args) {

		// double value in string format
		String to_double = "1234";

		Double doubleValue = new Double(to_double);

		System.out.println("String to Double value with Double constructor : " + doubleValue);

		to_double = "100d";

		doubleValue = new Double(to_double);

		System.out.println("String to Double value with d in the string using Double constructor : " + doubleValue);

	}
}

Output:
String to Double value with Double constructor : 1234.0
String to Double value with d in the string using Double constructor : 100.0

5. String with non integers


If the double string value has alphabets or non digits other than d then what will be the output.

Let us create the simple example program with abc.

Given the input as 1234abc and we are expecting to not to convert into double inappropriate values.
package com.javaprogramto.programs.strings.todouble;

public class StringToDoubleExample4 {

	public static void main(String[] args) {

		// double value in string format
		String to_double = "1234abc";

		Double doubleValue = Double.parseDouble(to_double);

		System.out.println("String to Double value with abc string : " + doubleValue);

	}
}

Output:

Let us run and see the what is the outcome.
Exception in thread "main" java.lang.NumberFormatException: For input string: "1234abc"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
	at java.base/java.lang.Double.parseDouble(Double.java:549)
	at com.javaprogramto.programs.strings.todouble.StringToDoubleExample4.main(StringToDoubleExample4.java:10)

Double valueOf() and parseDouble() method expect the input string to be in valid form of double otherwise NumberFormatException is expected to be thrown.

6. Conclusion


In this tutorial, we've seen how to convert the String values into double values in java.

By using third party api apache commons beans utils DoubleConverter class, string can be converted into double.




No comments:

Post a Comment

Please do not add any spam links in the comments section.