Archived

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

Dark Rain

Converting floats to string?

Recommended Posts

Dark Rain    157
Well seeing how the board search function block words like float and string I have to ask ^_^. There's no ftoa function and I cant seem to figure out how to make it work with a std::string. I'm sure it's a common enough problem that there's a standard function or something for that... right? [edited by - Dark Rain on June 7, 2002 3:07:24 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
Ok, listen carefully, here is the 'C++ way' : create a string stream, initialise it with the string containing the number, then read the fload off the stream.


    
#include <string>

#include <sstream>


std::string str = "100.5";
std::stringstream ss( str );
float f;
ss >> f;


Alternatively, if you are not adverse to downloading semi-standard libraries, head towards www.boost.org, and download their library.
They provide the boost/lexical_cast.hpp header which basically automates that operation for you in a C++-style cast. And works both ways :


        
#include <string>

#include <boost/lexical_cast.hpp>


std::string str = "1000.0";
float f = boost::lexical_cast<float>( str );

f = 500.254;
str = boost::lexical_cast<std::string>( f );


Now you're all set.

Note: make sure you use iostream and sstream (which defines std::stringstream, and uses std::string internally); not iostream.h or stringstream (which defines strstream and uses char* internally), which are older pre-standard versions.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 7, 2002 3:01:06 AM]

Share this post


Link to post
Share on other sites
Dark Rain    157
Thanks a lot, for the help!

EDIT : ummm I there's a small proble, I can only get it to work for string to float, not float to string.

I tried to the obvious way, inverting >> to <<, no dice. Any suggestions?

On another note, I think I might need a new C++ book, I never saw any mentions of string streams in it.

[edited by - Dark Rain on June 7, 2002 3:12:30 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
Actually, my first bit of code does the string->float translation.
Float to string would be :


    
std::stringstream ss;
ss << 1000.5;
std::string str = ss.str(); // IIRC, check your doc.



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 7, 2002 3:09:03 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
Note that initialising a stream with a string doesn't link the string to the stream... writing to the stream will not modify the string, you have to read it out of ss.str().

And unfortunately there is no 'clear' method for stringstreams (well there is, but it clears stream flags, not stream contents). So to reset the stream to the empty string, you have to assign it the empty string ( ss = "" );

Finally, you can have istringstream and ostringstream, just like ifstream and ofstream, istream (cin) and ostream (cout, cerr).

Have fun.

Edit: more 'stupid' stream tricks : the whole contents of any stream are stored into its 'read buffer', which you can access via stream::rdbuf().

So, while cout << cin is invalid, cout << cin.rdbuf() is not. And it works with files too (make a copy of a file -- fast and without a visible loop : ifstream ifs( "input" ); ofstream ofs( "output" ); ofs << ifs.rdbuf(); )

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 7, 2002 3:22:15 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
As for C++ books, I have (among many others) "The C++ programming language 3rd ed" (Stroustrup) and "The C++ Standard Library" (Josuttis).

Both mention string streams. I don''t understand why people don''t even know they exist (itoa, ftoa ?? Yuck !).

Make sure you have a look at the Boost library; it is full of good stuff.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by AndersO
Whats wrong with:

sprintf(buf,"%f",floatvalue);


It doesn''t work with well with std::string, we''re talking C++, not C. The whole point here is to get rid of char* buffers.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
FrigidHelix    122
quote:
Whats wrong with:

sprintf(buf,"%f",floatvalue);


The sprintf way is also less efficient and error prone since it needs to parse the format string to determine the type at runtime. Using the overloaded bitshift operators, the type is determined at compile time, enabling the correct input/output functions to be selected. Furthermore, sprintf cannot be extended for user defined types. Using the way, you can write output routines for your own classes.

Share this post


Link to post
Share on other sites