• Advertisement
Sign in to follow this  

string to Float

This topic is 2384 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 i change a string of contiguous number characters Ex: [color="#ff0000"]"-14.000" or[color="#48d1cc"] [color="#008000"]"14.000" . I mean[color="#ff0000"] string dog("-14.000"); or [color="#008000"]string dog("14.000");

What process would i use to change[color="#000080"]( [color="#ff0000"]string dog("-14.000"); or [color="#008000"]string dog("14.000"); [color="#000080"] ) into a ( [color="#ff0000"]float -14.000 or [color="#008000"]float 14.000 )

I'm using c++. Would I us some sort of type casting or is there a different way or more efficient way?

Share this post


Link to post
Share on other sites
Advertisement
You cannot cast to do this. One way it to use std::stringstream. Anther is to use std::sscanf(). If you have boost, then boost::lexical_cast<float>("14.000") is an option too.

Share this post


Link to post
Share on other sites
Something like this does the trick for me:
std::string mynumber = "0.123";
float myfloat = ::atof(mynumber.c_str());



Also... Search before posting ;)
http://www.gamedev.net/topic/401253-string-to-float/

Share this post


Link to post
Share on other sites
The ato*() functions are problematic because you cannot check for errors. It is up to you whether you consider this important.

Share this post


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

  • Advertisement