novice

R

Roman Töngi

How can I assign a double to a string?
I want to do something like this:

double val = 152.36;
string text(val);

This does of course not work.

Thanks
 
G

Gregor Razdrtic

double dval = 152.36;
string sval = atof((float)dval);

You should include proper headers.
 
E

Evan

Gregor's answer is incorrect; atof goes the opposite direction than
what you need. ("'atof' : cannot convert parameter 1 from 'float' to
'const char *'.")

Here's the best way I know of:

stringstream ss;
ss << val;
string text = ss.str();

You'll need to include <sstream>.

Stringstream is very useful for conversions like this. If you do it
often, check out boost.org's lexical_cast.
 
G

Gregor Razdrtic

Oh .... yes ... i wrote incorrect, let me fix this.
float to std::string
One solution follows

float fVar = 120.5;
char* tmpChar = new char[sizeof(float)+1];
sprintf(tmpChar, "%f", fVar);
std::string svar = tmpChar;
delete [] tmpChar;

Maybe not best , but working well
 
V

Victor Bazarov

Gregor Razdrtic said:
Oh .... yes ... i wrote incorrect, let me fix this.

First of all, plese don't top-post.
float to std::string
One solution follows

float fVar = 120.5;
char* tmpChar = new char[sizeof(float)+1];

'sizeof(float)' does not give you "how many bytes are needed for the
decimal representation of a float". Change your fVar to 120000.5 and
you will have undefined behaviour on your hands.

You have to use (fabs(log10(fVar)) + 8) (or something like that) to
determine how many chars to allocate.
sprintf(tmpChar, "%f", fVar);
std::string svar = tmpChar;
delete [] tmpChar;

Maybe not best , but working well

No, not working well. You allocate 5 chars, but print out 6 (five
to represent the '120.5' and the sixth is the terminating 0. You
simply write too much into 'tmpChar' than it has room.

V
 
C

codigo

Evan said:
Gregor's answer is incorrect; atof goes the opposite direction than
what you need. ("'atof' : cannot convert parameter 1 from 'float' to
'const char *'.")

Here's the best way I know of:

stringstream ss;
ss << val;
string text = ss.str();

You'll need to include <sstream>.

Stringstream is very useful for conversions like this. If you do it
often, check out boost.org's lexical_cast.

Or template a method for any type to generate a std::string:

#include <sstream>

template<class T>
std::string TtoStr(const T& r_t_)
{
std::eek:stringstream ossbuffer;
ossbuffer << r_t_;
return ossbuffer.str();
}
 
E

Evan

You can actually extend this idea to go from anything to anything:

template<typename target, typename source>
target convert(const source& s)
{
std::stringstream ss;
ss << s;

target T;
ss >> T;
return T;
}

Then use as such:
int a = convert<int>("4");
double b = convert<double>("4.56");
string s = convert<string>(4);

This is the general idea behind boost::lexical_cast
(http://www.boost.org/libs/conversion/lexical_cast.htm), but theirs is
much much more complicated, deals with error conditions, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top