meow00
asked on
how do I change a string to double ?
how do I change a string to double ? thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
b = Double.parseDouble(a);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Don't forget to import "NumberFormatException"...
Not required as it is in java.lang
Not required as it is in java.lang
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
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
:-)
ASKER
I have :
String a = "12.3";
double b;
how do I put a in b ?