Tutorial

Java Convert String to double

Published on August 3, 2022
Default avatar

By Pankaj

Java Convert String to double

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Java String to double conversion can be done by many ways. Today we will look into some common ways to convert java string to double primitive data type or Double object. Note that since java supports autoboxing, double primitive type and Double object can be used interchangeably without any issues.

Double d1 = 10.25d;
//autoboxing from double to Double
double d = Double.valueOf(10.25); 
//unboxing from Double to double

Java Convert String to Double

java convert string to double, java string to double Let’s look at all the different ways to convert string to double in java.

  1. Double.parseDouble()

    We can parse String to double using parseDouble() method. String can start with “-” to denote negative number or “+” to denote positive number. Also any trailing 0s are removed from the double value. We can also have “d” as identifier that string is a double value. This method returns double primitive type. Below code snippet shows how to convert string to double using Double.parseDouble() method.

    String str = "+123.4500d";
    double d = Double.parseDouble(str); // returns double primitive
    System.out.println(d); //-123.45, trailing 0s are removed
    
    System.out.println(Double.parseDouble("123.45001")); //123.45001
    System.out.println(Double.parseDouble("123.45001d")); //123.45001
    System.out.println(Double.parseDouble("123.45000")); //123.45
    System.out.println(Double.parseDouble("123.45001D")); //123.45001
    
  2. Double.valueOf()

    This method works almost similar as parseDouble() method, except that it returns Double object. Let’s see how to use this method to convert String to Double object.

    String str = "123.45";
    Double d = Double.valueOf(str); // returns Double object
    System.out.println(d); //123.45
    
    System.out.println(Double.valueOf("123.45d")); //123.45
    System.out.println(Double.valueOf("123.4500d")); //123.45
    System.out.println(Double.valueOf("123.45D")); //123.45
    
  3. new Double(String s)

    We can convert String to Double object through it’s constructor too. Also if we want double primitive type, then we can use doubleValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseDouble() or valueOf() methods.

    String str = "98.7";
    double d = new Double(str).doubleValue(); //constructor deprecated in java 9
    System.out.println(d); //98.7
    
  4. DecimalFormat parse()

    This is useful to parse formatted string to double. For example, if String is “1,11,111.23d” then we can use DecimalFormat to parse this string to double as:

    String str = "1,11,111.23d";
    try {
    	double l = DecimalFormat.getNumberInstance().parse(str).doubleValue();
    	System.out.println(l); //111111.23
    } catch (ParseException e) {
    	e.printStackTrace();
    }
    

    Note that parse() method returns instance of Number, so we are calling doubleValue() to get the double primitive type from it. Also this method throw ParseException if the string is not properly formatted.

That’s all for converting string to double in java program. Reference: Double API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 7, 2020

trying to run your code for DecimalFormat parse() it return exactly 1.11

- bryo

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 3, 2019

    Write syntax to convert the string value to double value

    - Cika

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 11, 2019

      trying to convert this string “10,00,30,00.30” but getting output as 1.00030003E7 can you help me with this? Thanks in advance

      - Neeraj singhal

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more
        DigitalOcean Cloud Control Panel