×

How to Convert String to double in Java

How to Convert String to double in java

It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from the text field, entered data is received as String, and we need to convert it from String to double. There are three methods to convert String to double:

  • Using Double.parseDouble(String) method
  • Using  Double.valueOf(String) method
  • Using the Constructor of Double class

Remember

  • All characters must be in digits.
  • The number can have a minus (-) sign.

Valid Conversions

  • String str=”23”;
  • String str=”-23.111”;

Invalid Conversions

  • String str=”23.54a”;
  • String str=” a23.90”;
  • String str=”abcd”;
  • String str=””;

If you defines a String as above, All the methods throw NumberFormatException, because it is not a valid conversion. 

Using Double.parseDouble() method

This is the static method of Double class. It returns the double primitive value. The signature of the method is given below.

public static double parseDouble(String s)

Where s is the String, we are passing to the method as an argument.

Example

In the following example, str is a variable of type String that number “12.037” as a string. The area is a variable of type double that stores the double value return by the parseDouble() method. Double is a wrapper class. parseDouble() is the static method of Double class and returns a double value. The println statement prints the converted value of the str variable.

public class StringToDoubleExample
{ 
public static void main(String args[])
{ 
String str="12.037"; 
double area=Double.parseDouble(str);  //conversion using parseDouble() method
System.out.println("Area is: " +area); 
}
}

Output

Area is: 12.037

Using Double.valueOf(String) method

This is the method of Double wrapper class. It is similar to parseDouble() method. It also returns the double primitive value.  The signature of the method is given below.

double variable_name = Double.valueOf(str);

Where str is the String, we are passing to the method as an argument.

Example

In the following example, var is a variable of type String that contains number “147.2369” as a String. value is a variable of type double that stores the double value return by the valueOf() method. Double is a wrapper class. valueOf() is the method passing var as an argument and returns a double value. The println statement prints the converted value of the variable var.

public class StringToDoubleExample1
{
public static void main(String args[])
{
String var = "147.2369";
double value = Double.valueOf(var); //conversion using valueOf() method
System.out.println(" Double value after conversion: "+value);
}
}

Output

Double value after conversion: 147.2369

Using the Constructor of Double class

Double class has a constructor that parses the String an argument and returns Double object. The signature of the method is given below.

public Double(String s)

Where s is the String, we are passing to the method as an argument.

Example

In this example, str is a variable of type String having a number “222.555” as a string. The number is a variable of type double that stores the double object return by the Double() constructor. Double is the constructor of Double class parses str as an argument. The println statement prints the converted value of the variable str.

public class StringToDoubleExample2
{
public static void main(String args[])
{
String str ="222.555";
double number = new Double(str); //conversion using constructor of Double class
System.out.println("Double value after conversion: "+number);
}
}

Output

Double value after conversion: 222.555

Related Topics

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.