Sign in to follow this  
pressgreen

string to Float

Recommended Posts

How would i change a string of contiguous number characters Ex: [color="#ff0000"]"-14.000"[/color] or[color="#48d1cc"] [color="#008000"]"14.000"[/color] [/color]. I mean[color="#ff0000"] string dog("-14.000");[/color] or [color="#008000"]string dog("14.000");[/color]

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

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
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:
[code]std::string mynumber = "0.123";
float myfloat = ::atof(mynumber.c_str());
[/code]


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this