Java Program: Convert String to Double

Write a Java program to convert a String to Double.

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!");


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:

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