Conversion from double to string (from newbie)

M

Marco Traverso

Hi all,
This may seem quite a basic question...
Is there a direct function to convert a double into a string? I'm used to
VBScript and it has the CStr function that does the job.

es. (vbScript)

Dim dN
Dim sString

dN =- Sqr(2)
sString = CStr(dN)

'sString returns "-1.4142135623731"

I've searched for a C / C++ function, and I found only the _ecvt
function (and other similar...).

Anyway they have slightly different input variables so the conversion is not
"automatic"

Is there a specific function or do I have to build my own?

Thank you in advance,

Marco
 
L

lallous

Marco Traverso said:
Hi all,
This may seem quite a basic question...
Is there a direct function to convert a double into a string? I'm used to
VBScript and it has the CStr function that does the job.

es. (vbScript)

Dim dN
Dim sString

dN =- Sqr(2)
sString = CStr(dN)

'sString returns "-1.4142135623731"

I've searched for a C / C++ function, and I found only the _ecvt
function (and other similar...).

Anyway they have slightly different input variables so the conversion is not
"automatic"

Is there a specific function or do I have to build my own?

Thank you in advance,

Marco
Hello,

Checkout the sprintf() function.
 
T

Thierry Miceli

Is there a direct function to convert a double into a string?
You can use string streams to do so:

#include <sstream>

std::eek:stringstream ostr;

double d = 123.456;
ostr << d;
cout << ostr.str();


Thierry
 
M

Marco Traverso

Thank you all for your replies!
I didn't know about the FAQ, so the next time I'll search through them
before posting! :)

Thanx again,

Marco
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top