Jump to content
  • Advertisement
Sign in to follow this  
pressgreen

string to Float

This topic is 2537 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!