Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Convert String to Double in Java



In this article, we will learn to convert a string to a double in Java

Problem Statement

Given a string representing a decimal number, convert it into a double in Java. The input string is guaranteed to be a valid numeric representation.

Input: String str = "23.6";
Output: 23 

Converting String to Double in Java

The following are the different approaches for converting a string to a double in Java -

Using Double.parseDouble()

The Double.parseDouble() method parses a string value to a double. It belongs to the Double wrapper class and is easier to use. This method accepts a string as an argument and returns its double value. If the given string cannot be parsed as a double for some reason, it throws a NumberFormatException.

Following is a syntax to convert the string to a double using parseDouble() in Java -

double res = Double.parseDouble("23.6");

Example

Below is an example of converting a string to a double using parseDouble() -

public class Demo {
   public static void main(String args[]){
      String str = "23.6";
      double res = Double.parseDouble(str);
      System.out.println("Double (String to Double) = "+res);
   }
}

Following is the output -

Double (String to Double) = 23.6

Time Complexity: O(1), as the conversion operation takes constant time.
Space Complexity: O(1), since no additional memory is allocated apart from storing the result.

Using Double.valueOf()

The Double.valueOf() method also converts a String to a double, but it returns an instance of double instead of a primitive double.

Following is the syntax to convert the string to a double using valueOf() in Java -

Double numObj = Double.valueOf(numberStr);

Example

Following is an example of converting a string to a double using valueOf() -

public class Main {
   public static void main(String[] args) {
      String numberStr = "99.99";
      Double numObj = Double.valueOf(numberStr);
      double num = numObj; // Unboxing
      System.out.println("Converted double: " + num);
   }
}

Following is the output -

Converted double: 99.99

String to Double Constructor

We can also use the Double(String s) constructor to convert a string to a double. This constructor creates a new Double object initialized to the value represented by the string argument.

Following is the syntax to convert a string to a double using the String to Double constructor in Java -

Double numObj = new Double("23.6");

Example

Below is an example of converting a string to a double using the String to Double constructor -

public class Main {
   public static void main(String[] args) {
      String numberStr = "99.99";
      Double numObj = new Double(numberStr);
      double num = numObj; // Unboxing
      System.out.println("Converted double: " + num);
   }
}

Following is the output of the above code -

Converted double: 99.99

Time Complexity: O(1), as the conversion operation takes constant time.
Space Complexity: O(1), because no additional memory is used for the conversion.

Using DecimalFormat

The DecimalFormat class is used to format decimal numbers in Java. It can also be used to convert a string to a double. Converting the string to a double using DecimalFormat in Java -

DecimalFormat df = new DecimalFormat("#.##");
double num = df.parse("23.6").doubleValue();

Example

Below is an example of converting a string to a double using DecimalFormat -

import java.text.DecimalFormat;
import java.text.ParseException;
public class Main {
   public static void main(String[] args) {
      String numberStr = "99.99";
      DecimalFormat df = new DecimalFormat("#.##");
      try {
         double num = df.parse(numberStr).doubleValue();
         System.out.println("Converted double: " + num);
      } catch (ParseException e) {
         e.printStackTrace();
      }
   }
}

Following is the output of the above code -

Converted double: 99.99

The complexity of this method is similar to the Double.parseDouble() method.

Updated on: 2025-05-29T19:21:03+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements