Double to string converstion in C++

AgentZap

Senior member
Sep 1, 2001
730
0
0
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.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
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 :(
 

Fistandantilis

Senior member
Aug 29, 2004
845
0
0

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
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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,

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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();

:)
 

itachi

Senior member
Aug 17, 2004
390
0
0
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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

itachi

Senior member
Aug 17, 2004
390
0
0
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.
 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
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! ;)
 

AgentZap

Senior member
Sep 1, 2001
730
0
0
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?
 

AgentZap

Senior member
Sep 1, 2001
730
0
0
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> >'
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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().
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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);
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
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).
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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)
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
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();
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
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::eek:stream from a quick lookup).
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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++