Java Program to convert String to Double



To convert String to Double in Java, you can use the following three examples. Two of them are using in-built methods parseDouble() and valueOf(). However, one of the method isn’t using a method.

Let us work through them one by one.

The following is an example to convert String to Double without using a method −

Example

 Live Demo

public class Demo {
    public static void main(String args[]) {
       Double val = new Double("88.77");
       System.out.println(val);
    }
}

Output

88.77

The following is another example to convert String to Double. Here, we will use parseDouble() method −

Example

 Live Demo

public class Demo {
    public static void main(String args[]) {
       double val = Double.parseDouble("11.89");
       System.out.println(val);
    }
}

Output

11.89

Let us see an example to convert String to Double using valueOf() method −

Example

 Live Demo

public class Demo {
    public static void main(String args[]) {
       Double val = Double.valueOf("299.9");
       System.out.println(val);
    }
}

Output

299.9
Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.


Advertisements