0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert String to Double - Simple Example

In Java, convert a string to a double using Double.parseDouble(yourString), which returns the double value represented by the string.
📋

Examples

Input"3.14"
Output3.14
Input"0"
Output0.0
Input"-123.456"
Output-123.456
🧠

How to Think About It

To convert a string to a double, think of reading the number as text and turning it into a decimal number type. Java provides a built-in method that reads the string and returns its double value if the string is a valid number.
📐

Algorithm

1
Take the input string that represents a number.
2
Use the built-in method to parse the string into a double.
3
Return the double value.
💻

Code

java
public class StringToDouble {
    public static void main(String[] args) {
        String numberStr = "3.14";
        double number = Double.parseDouble(numberStr);
        System.out.println(number);
    }
}
Output
3.14
🔍

Dry Run

Let's trace converting the string "3.14" to a double.

1

Input string

numberStr = "3.14"

2

Parse string

Double.parseDouble("3.14") returns 3.14

3

Print result

System.out.println(3.14) outputs 3.14

StepActionValue
1Input string"3.14"
2Parse to double3.14
3Print output3.14
💡

Why This Works

Step 1: Parsing the string

The method Double.parseDouble() reads the string and converts it to a double number.

Step 2: Valid string format

The string must represent a valid decimal number, otherwise it throws a NumberFormatException.

Step 3: Using the double value

Once converted, you can use the double value in calculations or print it.

🔄

Alternative Approaches

Using Double.valueOf()
java
public class StringToDoubleValueOf {
    public static void main(String[] args) {
        String numberStr = "3.14";
        double number = Double.valueOf(numberStr);
        System.out.println(number);
    }
}
Returns a Double object which auto-unboxes to double; useful if you need an object.
Using new Scanner
java
import java.util.Scanner;
public class StringToDoubleScanner {
    public static void main(String[] args) {
        String numberStr = "3.14";
        Scanner scanner = new Scanner(numberStr);
        double number = scanner.nextDouble();
        System.out.println(number);
        scanner.close();
    }
}
Uses Scanner to parse double from string; more flexible for complex input but heavier.

Complexity: O(n) time, O(1) space

Time Complexity

Parsing the string scans each character once, so it takes linear time relative to string length.

Space Complexity

No extra space is needed besides a few variables, so space is constant.

Which Approach is Fastest?

Double.parseDouble() is fastest and simplest; Double.valueOf() adds object creation overhead; Scanner is slower and more complex.

ApproachTimeSpaceBest For
Double.parseDouble()O(n)O(1)Simple, fast conversion
Double.valueOf()O(n)O(1)When Double object needed
Scanner.nextDouble()O(n)O(1)Parsing complex input streams
💡
Use Double.parseDouble() for quick and direct string to double conversion.
⚠️
Trying to convert a non-numeric string without handling exceptions causes runtime errors.