• Advertisement

Archived

This topic is now archived and is closed to further replies.

string to float

This topic is 5380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would you go about converting a string into a float?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Mishkin
In c++, use atof() and pass it a const char *

No ... in C, use atof(). In C++, use a stringstream.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Assuming you are using C. Try atof()

Share this post


Link to post
Share on other sites
Just for the record im using C++, and thanks for the quick replies but i have one more question now. What header is stringstream in?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I would''ve done it using atof, but the man page said it was deprecated and to use strtod.


char *s = ...;
char *end;
double d;

d = strtod(s, &end);

if (s == c) { /* no conversion */ }
else if (errno == ERANGE) { /* overflow/underflow */ }
else { /* d is valid */ }


This assumes errno isn''t set before invoking this code section.

Share this post


Link to post
Share on other sites
I included sstream and i get this error
error C2065: 'stringstream' : undeclared identifier


[edited by - SarsDP on May 1, 2003 11:16:31 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sscanf(string,"%f",&Float);

Share this post


Link to post
Share on other sites

  • Advertisement