Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a string into float or double 1

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
Hi, guys. I am having trouble converting numbers (which is in String datatype) into float or double. I need to add these numbers and return the sum of it to 2 decimal points.

I know this is quite easy but I have search in quite a lot of info but don't know exactly how to use any methods or class to do the above.

Pls advice, thanks.

Darryl H
 
you can use a wrapper class, in this case Double, note that this is a class and not a primitive double.

all you do to create a new Double instance by passing your string value (or you can use a double value - but obviously you can't). e.g.

Double myDouble = new Double(yourString);

you will then be able to mainpulate the Double object by using the methods associated with the class. For more info check out
which are the sun docs and create complete listings of available methods and properties for all java classes.

incidentally, if you wish to manipulate your double value as an actual primitive double type you can create a new variable of type double and set it equal to the value contained within the Double class e.g.

double myVariable = myDouble.doubleValue();

The doubleValue method of the Double wrapper class basically returns the value as a double.
hope this helps a bit.
good luck

[hammer]

p.s. there is similar Class type for float variables (in this case the wrapper class is of type Float) which also has a method called floatValue() blah blah blah.
 
double variable;
variable = Double.parseDouble(<string>);
 
Thanks, guys for the solutions.

Regards,

Darryl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top