View Full Version : converting String to double or float
nevarmore
09-25-2004, 08:02 PM
I'm starting with a string of raw data coming in from a device on the serial port. I have stripped that string down to the single number that I need, how do I go about converting that number from a string representation to a float or a double??
Regular typecasting does not work, as in:
float myFloater = (float)(myString);
sicarius
09-28-2004, 12:02 PM
try the itoa function....
kryptech.net
09-28-2004, 06:14 PM
doesnt that only work on integers? I think there is an implementaion of itofp(or something like that) in the K&R to convert them from floating point.
nevarmore
09-29-2004, 02:57 AM
That narrows down the search. Thanks guys, ill let you know what it is for sure.
sicarius
09-30-2004, 12:38 PM
actually...this is C#, for some reason I thought this was the C++ forum. In C# there is probably a more accurate method for conversion.
ntalektual
10-05-2004, 06:02 PM
you have to parse the string. I had to parse it as a double then cast it to a float, because for some reason there isn't a float class in the System namespace.
double dblNum = System.Double.Parse( Console.ReadLine() );
float fltNum = (float)dblNum;
I tried this and it worked for me. Hope this helps.
nevarmore
10-06-2004, 09:39 PM
Originally posted by sicarius
actually...this is C#, for some reason I thought this was the C++ forum. In C# there is probably a more accurate method for conversion.
I think it might. I'll let you know in a few weeks when I have a break from classes to work on my own projects. :D
Ethraim
12-15-2004, 11:49 AM
double fromStrToDbl = Convert.ToDouble("200");
float numberFlt = (float)fromStrToDou
will also work
nevarmore
12-15-2004, 03:34 PM
Thanks for posting that. This project has been pushed so far on the back burner its about to fall of the stove!!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.