English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)/O)

Java Reader/Writer

Java other topics

Java program converts string (string) variable to double

Java Examples Comprehensive

In this tutorial, we will learn how to convert a string variable to double in Java.

Example1Using parseDouble() method, Java program converts string to double

class Main {
  public static void main(String[] args) {
    //Create a string variable
    String str1 = "23";
    String str2 = "456.6";
    //Convert string to double precision
    //Using parseDouble()
    double num1 = Double.parseDouble(str1);
    double num2 = Double.parseDouble(str2);
    // Print double values
    System.out.println(num1);    // 23.0
    System.out.println(num2);    // 456.6
  }
}

In the above example, we use the parseDouble() method of the Double class to convert the string variable to double.

Here, Double is a wrapper class in Java. For more information, please visitJava Wrapper Classes.

NoteThe value of the string variable should be a number. Otherwise, the compiler will throw an exception. For example,

class Main {
  public static void main(String[] args) {
    //Create a string variable
    String str1 = "w3codebox"; //Not a number, but a string value
    // Convert string to double precision
    // Using parseDouble()
    double num1 = Double.parseDouble(str1);
    //Print Double Precision Value
    System.out.println(num1);    // throws NumberFormatException
  }
}

Example2Using valueOf() to convert a string to double precision in Java program

We can also use the valueOf() method to convert a string variable to a double precision type variable. For example,

class Main {
  public static void main(String[] args) {
    //Create a string variable
    String str1 = "6143";
    String str2 = "21312";
    //Convert String to Double
    //Using valueOf()
    double num1 = Double.valueOf(str1);
    double num2 = Double.valueOf(str2);
    //Print Double Precision Value
    System.out.println(num1);    // 6143.0
    System.out.println(num2);    // 21312.0
  }
}

In the above example, the valueOf() method of the Double class converts the string value to double.

Here, the valueOf() method actually returns an object of the Double class. However, the object will automatically be converted to the primitive type. In Java, this is called unboxing. For more information, please visitJava Auto-boxing and Unboxing.

That is,

//valueOf() returns a Double object
//object to double precision
double num1 = Double obj = Double.valueOf(str1);

Example3Java programs will convert strings containing commas to double

class Main {
  public static void main(String[] args) {
    //Create a string variable
    String str = "614,33";
    //Replace , with .
    str = str.replace(",", ".");
    //Convert String to Double
    //Using valueOf()
    double value = Double.parseDouble(str);
    //Print Double Precision Value
    System.out.println(value);    // 614.33
  }
}

In the above example, we created a string named str. Note this line,

str = str.replace(",", ".");

Here, the replace() method usesDot (.)Replace the string withComma (,).For more information on replacing characters, please visitJava String replace().

Then, we use the parseDouble() method to convert the string to double.

Java Examples Comprehensive