We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

how do I change a string to double ?

meow00
meow00 asked
on
Medium Priority
236 Views
Last Modified: 2010-03-31
how do I change a string to double ? thanks.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
double d = Double.parseDouble(s);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
 but I don't have a Double ...

  I have :

  String a = "12.3";

 double b;

 how do I put a in b ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
b = Double.parseDouble(a);
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

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

Not required as it is in java.lang
CERTIFIED EXPERT
Top Expert 2016

Commented:
More info on conversions, and testable, at

http://mindprod.com/applets/converter.html

Commented:
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


 



CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.