The Wayback Machine - https://web.archive.org/web/20241001062053/https://www.geeksforgeeks.org/convert-string-to-double-in-java/
Open In App

Convert String to Double in Java

Last Updated : 04 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Here, we will convert String to Double in Java. There are 3 methods for this Conversion from String to Double as mentioned below:

Example of String to Double Conversion

Input : String = “20.156”
Output: 20.156

Input : String = “456.21”
Output : 456.21

Methods for String to Double Conversion

Different Ways for Converting String to Double are mentioned below:

  1. Using the parseDouble() method of the Double class
  2. Using the valueOf() method of Double class
  3. Using the constructor of Double class 

1. Using parseDouble() Method of Double Class

The parseDouble() method of Java Double class is a built-in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.

Syntax

double str1 = Double.parseDouble(str); 

Java Program to Convert String to Double Using parseDouble() Method

Java




// Java program to convert String to Double
// Using parseDouble() Method of Double Class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Create and initializing a string
        String str = "2033.12244";
 
        // Converting the above string into Double
        // using parseDouble() Method
        double str1 = Double.parseDouble(str);
 
        // Printing string as Double type
        System.out.println(str1);
    }
}


Output

2033.12244

The complexity of the above method

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.

2. Using valueOf() Method of Double Class

The doubleValue() method of Double class is a built-in method to return the value specified by the calling object as double after type casting.

Syntax

double str1 = Double.valueOf(str); 

Java Program to Convert String to Double Using valueOf() Method

Java




// Java program to convert String to Double
// using valueOf() Method of Double Class
 
// Main class
public class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting the above string to Double type
        double str1 = Double.valueOf(str);
 
        // Printing above string as double type
        System.out.println(str1);
    }
}


Output

2033.12244

The complexity of the above method:

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.

3. Using the Constructor of Double Class

The Double class contains the constructor to intialize the Double objects using a String object.

Syntax

Double str1 = new Double(str);

Java Program to Convert String to Double Using Double Class Constructor

Java




// Java program to convert String to Double
// Using Constructor of Double class
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating and initializing a string
        String str = "2033.12244";
 
        // Converting above string into double type
        Double str1 = new Double(str);
 
        // print above string as Double type
        System.out.println(str1);
    }
}


Output

2033.12244

The complexity of the above method

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.



Previous Article
Next Article

Similar Reads

Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
Doubles.indexOf(double[] array, double[] target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double
3 min read
5 Ways to Convert Double to Integer in Java
Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double data type is the default data type for decimal numbers in Java and has a default size of 8 bytes. Example of Double double d1 = 1
5 min read
Java.Lang.Double Class in Java
Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the primitive type double which contains several methods
4 min read
Comparison of double and float primitive types in Java
Consider the following two codes in Java: Java Code // This program prints true class Geeksforgeeks { public static void main(String args[]) { float f = 5.25f; double d = 5.25 System.out.println(f == d); } } Output true Java Code // But this program prints false. class Geeksforgeeks { public static void main(String args[]) { float f = 5.1f; double
2 min read
Double byteValue() Method in Java with Examples
The java.lang.Double.byteValue() is a built-in method in Java that returns the value of this Double as a byte(by casting to a byte). Basically it used for narrowing primitive conversion of Double type to a byte value. Syntax: public byte byteValue() Parameters: The function does not accept any parameter. Return Value: This method returns the double
3 min read
Double.compareTo() Method in Java with Examples
The java.lang.Double.compareTo() is a built-in method in java that compares two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object
3 min read
Double.equals() Method in Java with Examples
The java.lang.Double.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Double object that contains the same double value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be prefer
2 min read
DoubleStream.Builder add(double t) in Java with Examples
DoubleStream.Builder add(double t) is used to insert an element into the element in the building phase of stream. It adds an element to the stream being built. Syntax: default DoubleStream.Builder add(double t) Parameters: This method accepts a mandatory parameter t which is the element to input into the stream. Exceptions: This method throws Illeg
2 min read
Double doubleValue() method in Java with examples
The doubleValue() method of Double class is a built in method to return the value specified by the calling object as double after type casting. Syntax: DoubleObject.doubleValue() Return Value: It return the value of DoubleObject as double. Below programs illustrate doubleValue() method in Java: Program 1: Java Code // Java code to demonstrate // Do
2 min read
Double intValue() method in Java with examples
The intValue() method of Double class is a built in method to return the value specified by the calling object as int after type casting. Syntax: DoubleObject.intValue() Return Value: It return the value of DoubleObject as int. Below programs illustrate intValue() method in Java: Program 1: // Java code to demonstrate // Double intValue() method cl
2 min read
Double shortValue() method in Java with examples
The shortValue() method of Double class is a built in method to return the value specified by the calling object as short after type casting.Syntax: DoubleObject.shortValue() Return Value: It return the value of DoubleObject as short.Below programs illustrate shortValue() method in Java: Program 1: Java Code // Java code to demonstrate // Double sh
2 min read
Double floatValue() in Java with Examples
The floatValue() method of Double class is a built-in method to return the value specified by the calling object as float after typecasting. Syntax: DoubleObject.floatValue() Return Type: It returns a float value i.e. the type casted value of the DoubleObject. Example 1: Java Code // Java Program to Implement floatValue() Method // of Double Class
1 min read
Double longValue() in Java with Examples
The longValue() method of Double class is a built in method to return the value specified by the calling object as long after type casting. Syntax DoubleObject.longValue() Parameters : It takes no parameters. Return type : It returns an long value i.e. the type casted value of the DoubleObject. Below is the implementation of the above method. Code
1 min read
Double hashCode() method in Java with examples
The hashCode() method of Double class is a built-in method use to return the hashcode value of this Double object. Syntax: DoubleObject.hashCode() Parameters: It takes no parameters. Return Type: It returns an int value. The value returned is (int)(v^(v>>>32)) where v is a long variable equal to Double.doubleToLongBits(this.doubleValue()).
1 min read
Double isInfinite() method in Java with examples
The java.lang.Double.isInfinite() method of Java Double class is a built in method in Java returns true if this Double value or the specified double value is infinitely large in magnitude, false otherwise. Syntax: public boolean isInfinite() or public static boolean isInfinite(double val) Parameters: The function accepts a single parameter val whic
2 min read
Double doubleToLongBits() method in Java with examples
The java.lang.Double.doubleToLongBits() method of Java Double class is a built-in function in java that returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout. Syntax: public static long doubleToLongBits(double val) Parameter: The method accepts only one parameter val which
2 min read
Double isNaN() method in Java with examples
The isNaN() method of Java Double class is a built in method in Java returns true if this Double value or the specified double value is Not-a-Number (NaN), or false otherwise. Syntax: public boolean isNaN() or public static boolean isNaN(double val) Parameters: The function accepts a single parameter val which specifies the value to be checked when
2 min read
Double doubleToRawLongBits() method in Java with examples
The doubleToRawLongBits() method of Java Double class is a built-in function in java that returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout preserving Not-a-Number (NaN) values. Syntax: public static long doubleToRawLongBits(double val) Parameter: The method accepts on
2 min read
Double parseDouble() method in Java with examples
The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double. Syntax: public static double parseDouble(String s) Parameters: It accepts a single mandatory parameter s which specifies the string to be pa
2 min read
Generate Infinite Stream of Double in Java
Given the task is to generate an infinite sequential unordered stream of double in Java. This can be done in following ways: Using DoubleStream.iterate(): Using the DoubleStream.iterate() method, iterate the DoubleStream with i by incrementing the value with 1. Print the DoubleStream with the help of forEach() method. import java.util.stream.*; pub
2 min read
OptionalDouble of(double) method in Java with examples
The of(double) method help us to get an OptionalDouble object which contains a double value which is passed as a parameter to this method. Syntax: public static OptionalDouble of(double value) Parameters: This method accepts a double value as parameter that will be set to the returned OptionalDouble object. Return value: This method returns an Opti
1 min read
Double colon (::) operator in Java
The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions. The only difference it has from lambda expressions is that this uses direct reference to the method by name instead of providing a delegate t
4 min read
PrintStream println(double) method in Java with Examples
The println(double) method of PrintStream Class in Java is used to print the specified double value on the stream and then break the line. This double value is taken as a parameter. Syntax: public void println(double doubleValue) Parameters: This method accepts a mandatory parameter doubleValue which is the double value to be written on the stream.
2 min read
PrintStream print(double) method in Java with Examples
The print(double) method of PrintStream Class in Java is used to print the specified double value on the stream. This double value is taken as a parameter. Syntax: public void print(double doubleValue) Parameters: This method accepts a mandatory parameter doubleValue which is the double value to be written on the stream. Return Value: This method d
2 min read
OptionalDouble orElse(double) method in Java with examples
The orElse(double) method helps us to get the value in this OptionalDouble object. If a value is not present in this OptionalDouble, then this method returns the value passed as the parameter. Syntax: public double orElse(double other) Parameters: This method accepts double the value to be returned, if no value is present. Return value: This method
1 min read
Initialize a static Map in Java using Double Brace Initialization
In this article, a static map is created and initialised in Java using Double Brace Initialization. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Double Brace Initialization In Double Brace Initialization: The first brace creates a new Anonymous
2 min read
ChoiceFormat nextDouble(double) method in Java with Examples
The nextDouble(double) method of java.text.ChoiceFormat class is used to get the double value just greater than the specified double value. Syntax: public static final double nextDouble(double d) Parameter: This method accepts double value d as a parameter for which the next double value is returned.Return Value: This method returns an double value
2 min read
ChoiceFormat nextDouble(double, boolean) method in Java with Examples
The nextDouble(double, boolean) method of java.text.ChoiceFormat class is used to get the double value just greater than the passed double value if passed Boolean value is true or it will return the double value just lesser than the passed double value if passed Boolean value is false. Syntax: public static double nextDouble(double d, boolean posit
2 min read
How to Set Precision For Double Values in Java?
Significant digits refer to all digits inclusive of left and right to decimal place keeping a note adding 0 to the left of any number is not countable as significant digits whereas precise digits refer to digits that are only lying on the right side or in short after the decimal place in mathematics. In Java, there are numbers more than 16 numbers
3 min read