• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Double to string converstion in C++

AgentZap

Senior member
I remember there were some functions for doing this, but I lost my notes on it from last semester.

1) The name of the function that returns the length of a string. Is it strlen? I forgot the syntax for it.

2) If I have a double and wish to convert it to a string, what is the simplest way? I need it to preserve the decimal point 2 two places in the string.

I found ecvt() on google, but I am either confused by it or it won't do exactly what I need.
 
1. Yes, strlen: size_t strlen(const char *s);
Unless you're using STL strings (which you really should!), in which case it's merely size()

2. You can use the C way - sprintf(char *str, const char *format, ...);
Or use ostringstream
 
Perhaps something like this? Obviously with some modifications to the data types, etc.

Maybe chop off the decimals and send it as a second variable.

Can only do so much with 24 weeks of high school programming 🙁
 

char name {10} = josh;
int length;
length = strlen(name)


I think that the easiest way to do any converting is with type casting:

double num;
char string;
num[10]

static_cast<num>(string)

it has been a while since C++ days but I hope this helps
 
Originally posted by: Fistandantilis
...
it has been a while since C++ days but I hope this helps

It shows 😉. The code you provided has sytax errors and won't even compile. Best follow Armitage's example and use ostringstream. For example,

 
I'm so glad they invented modern languages so I don't have to do this kind of crap anymore.

double x = 100.1;
string y = x.ToString();

🙂
 
x.ToString ()? double is a primitive type.. it has no member functions.

there's really no need for you to do this using the stl.. basic primitive type conversions shouldn't have to go through all the object construction/destruction crap when they don't need to.

sprintf (strvar, "%lf", &dblvar);
simple.. straight forward.. and gets the job done.
 
Originally posted by: itachi
x.ToString ()? double is a primitive type.. it has no member functions.

My compiler disagrees with you.

BTW, the capitalization in there is correct. "ToString()" rather than "toString()" and "string" rather than "String". This is not Java.
 
either you're running msvc with managed code (.net framework, which uses Double and String, just like java).. or you've got some no name compiler with no sense of ansi compliance. the '.' is a member selection operator.. primitives don't have members.
 
Originally posted by: Fistandantilis

char name {10} = josh;
int length;
length = strlen(name)


I think that the easiest way to do any converting is with type casting:

double num;
char string;
num[10]

static_cast<num>(string)

it has been a while since C++ days but I hope this helps

hoo boy... you should be banned from giving out any more c++ advice evar! 😉
 
Originally posted by: singh
Originally posted by: Fistandantilis
...
it has been a while since C++ days but I hope this helps

It shows 😉. The code you provided has sytax errors and won't even compile. Best follow Armitage's example and use ostringstream. For example,

The compiler doesn't like the .str() in your example

... error C2228: left of '.str' must have class/struct/union type

Additionally, what does "oss" do?
 
Originally posted by: BingBongWongFooey
#include <sstream>

...

double d = 5.4;
std::string s = (std::stringstream() << d).str();

When I try that example I get error C2039: 'str' : is not a member of 'basic_ostream<char,struct std::char_traits<char> >'
 
Originally posted by: itachi
either you're running msvc with managed code (.net framework, which uses Double and String, just like java).. or you've got some no name compiler with no sense of ansi compliance. the '.' is a member selection operator.. primitives don't have members.

Yeah, it's C#, which uses boxing to automatically convert from primitives to objects.
Go ahead and compile the two lines above in VS.NET from a .cs file, it works fine.
 
Originally posted by: AgentZap
Originally posted by: BingBongWongFooey
#include <sstream>

...

double d = 5.4;
std::string s = (std::stringstream() << d).str();

When I try that example I get error C2039: 'str' : is not a member of 'basic_ostream<char,struct std::char_traits<char> >'

WTF, same thing on gcc. I swear I've used that in the past, and as far as I can see it should work perfectly. Been a while since I've done any C++ though.
 
Originally posted by: AgentZap
Originally posted by: singh
Originally posted by: Fistandantilis
...
it has been a while since C++ days but I hope this helps

It shows 😉. The code you provided has sytax errors and won't even compile. Best follow Armitage's example and use ostringstream. For example,

The compiler doesn't like the .str() in your example

... error C2228: left of '.str' must have class/struct/union type

Additionally, what does "oss" do?



Sorry, that should be oss.str() not dvalue.str().
 
Originally posted by: singh
Originally posted by: AgentZap
Originally posted by: singh
Originally posted by: Fistandantilis
...
it has been a while since C++ days but I hope this helps

It shows 😉. The code you provided has sytax errors and won't even compile. Best follow Armitage's example and use ostringstream. For example,

The compiler doesn't like the .str() in your example

... error C2228: left of '.str' must have class/struct/union type

Additionally, what does "oss" do?



Sorry, that should be oss.str() not dvalue.str().

To expand...
oss is an ostringstream object. You treat it like a stream to an output file or the standard out stream, including all the stream formatting stuff.

But instread of going to a file, or your screen, etc., it goes into an STL string, which you can then retrieve with the .str() member function.

I use them all the time to build sql queries.
 
Originally posted by: notfred
I'm so glad they invented modern languages so I don't have to do this kind of crap anymore.

double x = 100.1;
string y = x.ToString();

🙂

A couple of lines of code is hardly anything to fret over. Besides, most experienced programmers have their own library of code they've developed over the years. All you have to do is include it in and it could be as simple as:

string num = toString(dvalue);
 
Originally posted by: BingBongWongFooey
Originally posted by: AgentZap
Originally posted by: BingBongWongFooey
#include <sstream>

...

double d = 5.4;
std::string s = (std::stringstream() << d).str();

When I try that example I get error C2039: 'str' : is not a member of 'basic_ostream<char,struct std::char_traits<char> >'

WTF, same thing on gcc. I swear I've used that in the past, and as far as I can see it should work perfectly. Been a while since I've done any C++ though.

The code does work. I've been writing quite a bit of C++ code for the last year, and that's exactly how you would convert a double into a string (aside from possibly setting the precision you're interested in).
 
Originally posted by: oog
Originally posted by: BingBongWongFooey
Originally posted by: AgentZap
Originally posted by: BingBongWongFooey
#include <sstream>

...

double d = 5.4;
std::string s = (std::stringstream() << d).str();

When I try that example I get error C2039: 'str' : is not a member of 'basic_ostream<char,struct std::char_traits<char> >'

WTF, same thing on gcc. I swear I've used that in the past, and as far as I can see it should work perfectly. Been a while since I've done any C++ though.

The code does work. I've been writing quite a bit of C++ code for the last year, and that's exactly how you would convert a double into a string (aside from possibly setting the precision you're interested in).

I think it needs to be ostringstream instead of just stringstream
 
Why not just create a Double (capital D) with the value and then do .ToString()?

double x = 42.42;
Double d = new Double(x);

d.ToString();
 
Originally posted by: BingBongWongFooey
Originally posted by: Armitage
I think it needs to be ostringstream instead of just stringstream

Tried it, made no difference.

(stringstream is a superset of ostringstream anyways, afaik)


The reason is because operator << doesn't return an object which has a str() member function (it returns std:😱stream from a quick lookup).
 
Originally posted by: joshsquall
Why not just create a Double (capital D) with the value and then do .ToString()?

double x = 42.42;
Double d = new Double(x);

d.ToString();

Because the subject is C++
 
Back
Top