Advertisement

number to string

Started by May 26, 2002 05:44 PM
16 comments, last by Lode 22 years, 5 months ago
How to convert a number to a string? I want to do something like: double a = 66.66; string b = a; put string b on screen; then I''d like to see the output 66.66 on screen, but the above code outputs B, that has the ascii value 66
Look up the atof function...
Advertisement
atof converts string to a number.
You are looking for sprintf().
Use sprintf


  char buf[256];sprintf( buf, "%f", number );  


Or use boost::lexical_cast


  #include <boost/lexical_cast.hpp>double a = 66.66;string b = boost::lexical_cast<double>( a )  


Note: atof does the conversion in the wrong direction.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Great, with sprintf it works great and I can easily convert the char to a string then.

boost, however, compiled correctly (after I found all the additional needed files), but the exe crashed when executing it, and this only when the boost::lexical_cast( a ) line is there. Not really a problem anyway, I'll stick with sprintf

[edited by - Lode on May 26, 2002 7:16:54 PM]
_gcvt() works as well
Advertisement
quote: Original post by Anonymous Poster
_gcvt() works as well

Under only a single compiler, it''s about as far as you can get from standard. Another possible answer is stringstream (for C++, not C), feel free to look it up on your own.

sprintf is ugh.

Try

  #include <iostream>#include <sstream>#include <string>//...double a = 66.66;std::ostringstream oss;oss << a;std::string b = oss.str();std::cout << b;  



--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote: Original post by Lode
boost, however, compiled correctly (after I found all the additional needed files), but the exe crashed when executing it, and this only when the boost::lexical_cast( a ) line is there. Not really a problem anyway, I''ll stick with sprintf


Sorry, should have been boost::lexical_cast<string>( a ) ... because you''re casting to a string, not a double...


  double a = 66.66;string b = boost::lexical_cast<string>( a );double c = boost::lexical_cast<double>( b );  



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Arild Fines
sprintf is ugh.

Try

#include <iostream>
#include <sstream>
#include <string>

//...

double a = 66.66;
std::ostringstream oss;
oss << a;

std::string b = oss.str();

std::cout << b;


Give me a freaking break. All the rabid STL fans out there are making me sick. If we wanna use deprecated libraries, LET US! Your code is twice as ugly and complex as the sprintf example.

Later,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

EDITED for formatting

[edited by - zealouselixir on May 26, 2002 11:08:42 PM]

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement