beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Convert String to Double examples

By Chaitanya Singh | Filed Under: Java Conversion

In this guide we will see how to convert String to Double in Java. There are three ways to convert String to double.

1. Java – Convert String to Double using Double.parseDouble(String) method
2. Convert String to Double in Java using Double.valueOf(String)
3. Java Convert String to double using the constructor of Double class – The constructor Double(String) is deprecated since Java version 9

1. Java Convert String to Double using Double.parseDouble(String)

public static double parseDouble(String str) throws NumberFormatException

This method returns the double representation of the passed String argument. This method throws NullPointerException, if the specified String str is null and NumberFormatException – if the string format is not valid.  For example, if the string is “122.20ab” this method would throw NumberFormatException.

String str="122.202";
double dnum = Double.parseDouble(str);

The value of variable dnum of double type would be 122.202 after conversion.

Lets see the complete example of the conversion using parseDouble(String) method.

Example 1: Java Program to convert String to double using parseDouble(String)

public class JavaExample{
   public static void main(String args[]){
	String str = "122.202";

	/* Convert String to double using 
	 * parseDouble(String) method of Double
	 * wrapper class
	 */
	double dnum = Double.parseDouble(str);
		
	//displaying the value of variable dnum
	System.out.println(dnum);
   }
}

Output:
Java Convert String to double using parseDouble()

2. Java Convert String to Double using Double.valueOf(String)

The valueOf() method of Double wrapper class in Java, works similar to the parseDouble() method that we have seen in the above java example.

String str = "122.111";
double dnum = Double.valueOf(str);

The value of dnum would be 122.111 after conversion

Lets see the complete example of conversion using Double.valueOf(String) method.

Example 2: Java Program to convert String to double using valueOf(String)

public class JavaExample{
   public static void main(String args[]){
	String str = "122.111";

	/* Convert String to double using 
	 * valueOf(String) method of Double
	 * wrapper class
	 */
	double dnum = Double.valueOf(str);
		
	//displaying the value of variable dnum
	System.out.println(dnum);
   }
}

Output:
Convert String to double in Java using valueOf()

3. Java Convert String to double using the constructor of Double class

Note: The constructor Double(String) is deprecated since Java version 9

String str3 = "999.333";
double var3 = new Double(str3);

Double class has a constructor which parses the String argument that we pass in the constructor, and returns an equivalent double value.

public Double(String s) throws NumberFormatException

Using this constructor we can create a new object of the Double class by passing the String that we want to convert.

Example 3: Java Program to convert String to double using the constructor of Double class

In this example we are creating an object of Double class to convert the String value to double value.

public class Example{
   public static void main(String args[]){
       String str3 ="999.333";
       double var3 = new Double(str3);
       System.out.println(var3);
   }
}

Output:

999.333

References:

  • parseDouble(String) JavaDoc
  • Double.valueOf(String) JavaDoc
  • new Double(String) JavaDoc
❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap