• Advertisement
Sign in to follow this  

How do I convert a std::string to a float?

This topic is 4686 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

Advertisement
std::stringstream


#include <sstream>

float ToFloat(const std::string s)
{
float n;
std::stringstream ss(s);
ss >> n;
return n;
}

Share this post


Link to post
Share on other sites
Or, if you have boost:

std::string string_num = "3.141";
float float_num = boost::lexical_cast<float>(string_num);

Jim.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

what i use:

float stof(std::string in)
{
//revision from http://www.jbox.dk/sanos/source/lib/strtod.c.html
//Since mine is just a revision(in my opinion better) of the atof shown there, posting the whole thing would be useless
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement