Convert String To Double in Java

Converting a String to a Double is a common operation in Java, frequently used when processing user input or reading data from external sources like files or databases. A double data type is a 64-bit floating-point value that can represent a wide range of decimal numbers with higher precision compared to the float data type. In this blog post, we will explore different methods to convert a String to a double and provide practical examples with outputs to illustrate each approach.

Method 1: Using Double.parseDouble() Method 

The Double.parseDouble() method parses the string argument as a decimal double and returns the resulting numeric value.

Example:

public class StringToDoubleExample {
    public static void main(String[] args) {
        String strNumber = "3.14159";

        try {
            // Convert String to Double using parseDouble()
            double doubleValue = Double.parseDouble(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Double Value: " + doubleValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for double conversion");
        }
    }
}

Output:

Input String: 3.14159
Double Value: 3.14159

Method 2: Using Double.valueOf() Method 

The Double.valueOf() method converts a String to a Double object. It returns a Double instance representing the specified double value if the String is a valid representation of a double. 

Example:

public class StringToDoubleExample {
    public static void main(String[] args) {
        String strNumber = "10.5678";

        try {
            // Convert String to Double using valueOf()
            Double doubleValue = Double.valueOf(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Double Value: " + doubleValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for double conversion");
        }
    }
}

Output:

Input String: 10.5678
Double Value: 10.5678

Method 3: Using NumberFormat.parse() Method 

Similar to converting to a Float, you can use NumberFormat to parse the String and convert it to a Double. 

Example:
import java.text.NumberFormat;
import java.text.ParseException;

public class StringToDoubleExample {
    public static void main(String[] args) {
        String strNumber = "99.999";

        // Convert String to Double using NumberFormat
        try {
            NumberFormat numberFormat = NumberFormat.getInstance();
            double doubleValue = numberFormat.parse(strNumber).doubleValue();
            System.out.println("Input String: " + strNumber);
            System.out.println("Double Value: " + doubleValue);
        } catch (ParseException e) {
            System.out.println("Error: Invalid input for double conversion");
        }
    }
}

Output:

Input String: 99.999
Double Value: 99.999

Conclusion

Converting a String to a Double is a fundamental operation in Java. You can use methods like Double.parseDouble(), Double.valueOf(), or NumberFormat to achieve this. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Always handle exceptions properly to handle invalid input gracefully and ensure the reliability of your code.

Comments