Java Program: Convert String to Double


Write a Java program to convert a String to Double.

Solution:
public class Example {

    public static void main(String[] args) {

        //Input String
        String numberAsString = "10000.50";

        try {
            double doubleNumber = Double.parseDouble(numberAsString);
            System.out.println("String: " + numberAsString);
            System.out.println("Double: " + doubleNumber);
        } catch (NumberFormatException e) {
            System.err.println("Invalid number as String!");
            e.printStackTrace();
        }

    }
}
Output:

String: 10000.50
Double: 10000.5

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap