# converting from char to float and float to char

## Recommended Posts

How do you convert from char to float so that if the char array has 2.02 stored in it I can make the float variable = 2.02? [Edited by - HarrisonPloeg on March 6, 2007 3:33:56 AM]

##### Share on other sites
char flt [] = "129.30";
float f = atof(flt);

##### Share on other sites
How do you do float to char?

##### Share on other sites
http://www.c-for-dummies.com/lessons/float2string/

##### Share on other sites
hii..
To convert from float to char use the function fcvt
ex.
char *fps=NULL;
int dec,sign;
float LastFrameRate = 12.33f

fps=_fcvt(LastFrameRate,2,&dec,&sign);

The number 2 specifies the number of digits after the decimal point.dont worry about the last 2 parameters..

just in case u need to convert from integer to char..its itoa and from char to integer its atoi...

hope this helps...

##### Share on other sites
Unless you are using C++, in which case use strings and stringstreams:

#include <sstream>float str_to_float(const std::string &in) // you can still pass a const char* to this{    std::ostringstream o(in); float f; o >> f; return f;}std::string float_to_str(float f){    std::istringstream i; i << f; return i.str();}

Or you could not reinvent this particular wheel, and use boost::lexical_cast.

##### Share on other sites
First tell me what you think a char is. Note that a char is not the same thing as an array of char. Also, an array of char is not "a string", in a useful sense - if you're thinking of strings, you should be using real strings.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628281
• Total Posts
2981800

• 10
• 11
• 17
• 14
• 9