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 ?

on
Medium Priority
236 Views
how do I change a string to double ? thanks.
Comment
Watch Question

## View Solutions Only

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

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

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);
Principal 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
Principal Technologist
CERTIFIED EXPERT

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

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

Commented:

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:
:-)
##### Thanks for using Experts Exchange.

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