Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 11, 2015

How do I convert a double into a string in C++?

How do I convert a double into a string in C++?


I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).

Answer by Darron for How do I convert a double into a string in C++?


Take a look at sprintf() and family.

Answer by Firas Assaad for How do I convert a double into a string in C++?


You could also use stringstream.

Answer by Adam Rosenfield for How do I convert a double into a string in C++?


// The C way:  char buffer[32];  snprintf(buffer, sizeof(buffer), "%g", myDoubleVar);    // The C++03 way:  std::ostringstream sstream;  sstream << myDoubleVar;  std::string varAsString = sstream.str();    // The C++11 way:  std::string varAsString = std::to_string(myDoubleVar);    // The boost way:  std::string varAsString = boost::lexical_cast(myDoubleVar);  

Answer by Konrad Rudolph for How do I convert a double into a string in C++?


If you use C++, avoid sprintf. It's un-C++y and has several problems. Stringstreams are the method of choice, preferably encapsulated as in Boost.LexicalCast which can be done quite easily:

template   std::string to_string(T const& value) {      stringstream sstr;      sstr << value;      return sstr.str();  }  

Usage:

string s = to_string(42.5);  

Answer by Vinko Vrsalovic for How do I convert a double into a string in C++?


Heh, I just wrote this (unrelated to this question):

string temp = "";  stringstream outStream;  double ratio = (currentImage->width*1.0f)/currentImage->height;  outStream << " R: " << ratio;  temp = outStream.str();    /* rest of the code */  

Answer by coppro for How do I convert a double into a string in C++?


sprintf is okay, but in C++, the better, safer, and also slightly slower way of doing the conversion is with stringstream:

#include   #include     // In some function:  double d = 453.23;  std::ostringstream os;  os << d;  std::string str = os.str();  

You can also use Boost.LexicalCast:

#include   #include     // In some function:  double d = 453.23;  std::string str = boost::lexical_cast(d);  

In both instances, str should be "453.23" afterward. LexicalCast has some advantages in that it ensures the transformation is complete. It uses stringstreams internally.

Answer by Johannes Schaub - litb for How do I convert a double into a string in C++?


The boost (tm) way:

std::string str = boost::lexical_cast(dbl);  

The Standard C++ way:

std::ostringstream strs;  strs << dbl;  std::string str = strs.str();  

Note: Don't forget #include

Answer by Fred Larson for How do I convert a double into a string in C++?


Herb Sutter has an excellent article on string formatting. I recommend reading it. I've linked it before on SO.

Answer by Mr.Ree for How do I convert a double into a string in C++?


You may want to read my prior posting on SO. (Macro'ed version with a temporary ostringstream object.)

For the record: In my own code, I favor snprintf(). With a char array on the local stack, it's not that inefficient. (Well, maybe if you exceeded the array size and looped to do it twice...)

(I've also wrapped it via vsnprintf(). But that costs me some type checking. Yelp if you want the code...)

Answer by Foxhunter for How do I convert a double into a string in C++?


The problem with lexical_cast is the inability to define precision. Normally if you are converting a double to a string, it is because you want to print it out. If the precision is too much or too little, it would affect your output.

Answer by kennytm for How do I convert a double into a string in C++?


The Standard C++11 way:

#include     auto str = std::to_string(42.5);   

to_string is a new library function introduced in N1803 (r0), N1982 (r1) and N2408 (r2) "Simple Numeric Access". There are also the stod function to perform the reverse operation.

Answer by Mohamed Tahir for How do I convert a double into a string in C++?


The easiest, straight forward way is this:

s=format("%f",d);  

where d is the float or double and s is the string you will be getting.

Answer by Alexis Snchez Tello for How do I convert a double into a string in C++?


You could try a more compact style:

std::string number_in_string;    double number_in_double;    std::ostringstream output;    number_in_string = (dynamic_cast< std::ostringstream*>(&(output << number_in_double <<    std::endl)))->str();   

Answer by DannyK for How do I convert a double into a string in C++?


I would like at the C++ String Toolkit Libary. Just posted a similar answer elsewhere. I have found it very fast and reliable.

#include     double pi = M_PI;  std::string pi_as_string  = strtk::type_to_string( pi );  

Answer by Ingo for How do I convert a double into a string in C++?


Normaly for this operations you have to use the ecvt, fcvt or gcvt Functions:

/* gcvt example */  #include   #include     main ()  {    char buffer [20];    gcvt (1365.249,6,buffer);    puts (buffer);    gcvt (1365.249,3,buffer);    puts (buffer);    return 0;  }    Output:  1365.25  1.37e+003     

As a Function:

void double_to_char(double f,char * buffer){    gcvt(f,10,buffer);  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.