how do I change a string to double ?

Posted on 2006-04-25
Last Modified: 2010-03-31
how do I change a string to double ? thanks.
Question by:meow00
    LVL 86

    Accepted Solution

    double d = Double.parseDouble(s);
    LVL 1

    Author Comment

     but I don't have a Double ...

      I have :

      String a = "12.3";

     double b;

     how do I put a in b ?
    LVL 86

    Expert Comment

    b = Double.parseDouble(a);
    LVL 30

    Expert Comment


    Assisted Solution

    Simplest use:

    String myString = "12.3";
    try {
       double myDouble = Double.parseDouble(myString);
    } catch (NumberFormatException e) {
       //The string does not contain a double value...

    Don't forget to import "NumberFormatException"...

    Best regards,
    LVL 30

    Expert Comment

    >> Don't forget to import "NumberFormatException"...

    Not required as it is in java.lang
    LVL 86

    Expert Comment

    More info on conversions, and testable, at

    Expert Comment

    I'm sure you have the answer by now

    You often have the situation where you want to convert a String representing a number to a primitive.

    There are reference classes for each of the primitive types, and these provide static methods for converting
    a String representing the number (like "12.0")  to the primitive - 12.0 ( A double)

    So in answer to your question  "how do I put a in b ?" CEHJ is quite right, but you might not be aware
    that you don't need any existing references as the conversion methods are static.

    Java provides static String conversion methods for many of the primitive types, by the way :

     double d = Double.parseDouble("12");   //12.0
     float f = Float.parseFloat("12");            //12.0
     long l = Long.parseLong("12");             // 12
     int i =    Integer.parseInt("12");            // 12

     int i =    Integer.parseInt("12.0");            // throws a NumberFormatException (as KK said)
    Hope this helps


    LVL 86

    Expert Comment


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsWithoutList  challenge 24 62
    Increment alphanumeric sequence 6 58
    bunnyEars2 challenge 6 48
    noX challenge 17 55
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now