how do I change a string to double ?

how do I change a string to double ? thanks.
LVL 1
meow00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
double d = Double.parseDouble(s);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
meow00Author Commented:
 but I don't have a Double ...

  I have :

  String a = "12.3";

 double b;

 how do I put a in b ?
CEHJCommented:
b = Double.parseDouble(a);
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mayank SAssociate Director - Product EngineeringCommented:
KriskraCommented:
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,
KK
Mayank SAssociate Director - Product EngineeringCommented:
>> Don't forget to import "NumberFormatException"...

Not required as it is in java.lang
CEHJCommented:
More info on conversions, and testable, at

http://mindprod.com/applets/converter.html
nosturnCommented:
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
Andy


 



CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.