We help IT Professionals succeed at work.

String to float

koger
koger asked
on
How do I convert a string value to a float.
A small example would be great :)

Comment
Watch Question

use the atof function:

float fVal = atof(strName);

Commented:
Here is the code to convert string to double. (There no api available to convert the string to float directly.) Here is the code sample:

#include <stdlib.h>

main()
{
  char* str; *stopstr;

}
Commented:
Here is the code to convert string to double. (There no api available to convert the string to float directly.) Here is the code sample:

#include <stdlib.h>
#include <iostream.h>

main()
{
  char* str; *stopstr;
  double dRet;
  str = "3.145"; //This is in string format.
  dRet = strtol(str, stopstr); //Convert the string to double.
 cout<<dRet;

}


Hope this helps you.

Commented:
Hi,
sorry, you have to use "strtod" instead of "strtol". Typing mistake.

Thangs
=> koger
Would you mind telling me why you didn't like my answer?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.