Convert String To Double in Java

Introduction

Converting a String to a Double in Java is a common task that can be useful in various scenarios, such as parsing user input, reading data from files, or handling configuration parameters. In Java, this conversion can be done using several methods. This blog post will explore different methods to convert a String to a Double in Java.

Table of Contents

  1. Using Double.parseDouble()
  2. Using Double.valueOf()
  3. Using DecimalFormat
  4. Complete Example Program
  5. Conclusion

1. Using Double.parseDouble()

The Double.parseDouble() method parses the string argument as a double.

Example:

public class StringToDoubleUsingParseDouble {
    public static void main(String[] args) {
        String strValue = "123.45";

        // Convert string to double using Double.parseDouble()
        double doubleValue = Double.parseDouble(strValue);

        System.out.println("String value: " + strValue);
        System.out.println("Double value: " + doubleValue);
    }
}

Output:

String value: 123.45
Double value: 123.45

Explanation:

  • Double.parseDouble(strValue) converts the string value to its double representation.

2. Using Double.valueOf()

The Double.valueOf() method returns a Double instance representing the specified string value. This method can also be used to convert a String to a double.

Example:

public class StringToDoubleUsingValueOf {
    public static void main(String[] args) {
        String strValue = "678.90";

        // Convert string to double using Double.valueOf()
        double doubleValue = Double.valueOf(strValue);

        System.out.println("String value: " + strValue);
        System.out.println("Double value: " + doubleValue);
    }
}

Output:

String value: 678.90
Double value: 678.9

Explanation:

  • Double.valueOf(strValue) converts the string value to its double representation.

3. Using DecimalFormat

The DecimalFormat class allows you to parse a string into a double using a specific pattern.

Example:

import java.text.DecimalFormat;
import java.text.ParseException;

public class StringToDoubleUsingDecimalFormat {
    public static void main(String[] args) {
        String strValue = "1234.56";

        // Convert string to double using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        try {
            double doubleValue = decimalFormat.parse(strValue).doubleValue();

            System.out.println("String value: " + strValue);
            System.out.println("Double value: " + doubleValue);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output:

String value: 1234.56
Double value: 1234.56

Explanation:

  • decimalFormat.parse(strValue).doubleValue() converts the string value to its double representation using the specified pattern.

4. Complete Example Program

Here is a complete program that demonstrates all the methods discussed above to convert a String to a Double.

Example Code:

import java.text.DecimalFormat;
import java.text.ParseException;

public class StringToDoubleExample {
    public static void main(String[] args) {
        String strValue1 = "123.45";
        String strValue2 = "678.90";
        String strValue3 = "1234.56";

        // Using Double.parseDouble() Method
        double doubleValue1 = Double.parseDouble(strValue1);
        System.out.println("Using Double.parseDouble():");
        System.out.println("String value: " + strValue1 + " -> Double value: " + doubleValue1);

        // Using Double.valueOf() Method
        double doubleValue2 = Double.valueOf(strValue2);
        System.out.println("\nUsing Double.valueOf():");
        System.out.println("String value: " + strValue2 + " -> Double value: " + doubleValue2);

        // Using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        try {
            double doubleValue3 = decimalFormat.parse(strValue3).doubleValue();
            System.out.println("\nUsing DecimalFormat:");
            System.out.println("String value: " + strValue3 + " -> Double value: " + doubleValue3);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output:

Using Double.parseDouble():
String value: 123.45 -> Double value: 123.45

Using Double.valueOf():
String value: 678.90 -> Double value: 678.9

Using DecimalFormat:
String value: 1234.56 -> Double value: 1234.56

5. Conclusion

Converting a String to a Double in Java can be accomplished in several ways. The Double.parseDouble() and Double.valueOf() methods are both straightforward and widely used. Using DecimalFormat provides additional control over the formatting of the double value. By understanding these different methods, you can choose the one that best fits your needs and coding style.

Happy coding!

Comments