Conversion From String to Double in Java

You are currently viewing Conversion From String to Double in Java

Conversion from String to Double in Java

//Java code to convert String to Double
public class Main {
    public static void main(String args[]) {
        String str = "12345.87878d";

        //variable to store result
        double result = 0;

        //converting string to double
        //method 1
        result = Double.valueOf(str).doubleValue();
        System.out.println("result (value of str as double) = " + result);

        //method 2
        result = Double.parseDouble(str);
        System.out.println("result (value of str as double) = " + result);
    }
}

Output

result (value of str as double) = 12345.87878
result (value of str as double) = 12345.87878

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply