×

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

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.