Double to string converstion in C++

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Originally posted by: Armitage
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++

I could have sworn C++ had the Double data type. Guess I'm thinking of Java.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: joshsquall
Originally posted by: Armitage
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++

I could have sworn C++ had the Double data type. Guess I'm thinking of Java.

Definitely.
 

Fistandantilis

Senior member
Aug 29, 2004
845
0
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)

show me the way, whats wrong with this code, I obviosly need more help than this guy:eek:

this is how I woul have done it and it may be wrong but let me try:





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


#include <iostream>
using namespace std;

int main()
char name;
int length;
{
cout << "What is your name?" << endl;
cout << "And I will tell you how long it is" << endl;
cin >> name;
length = strlen(name);
cout << "Your name is " << length << endl;
return 0
}

like I said it has been a while and I will install C++ tonight to tighten up my coding, sorry for being misleading
 

AgentZap

Senior member
Sep 1, 2001
730
0
0
Yep, got it now.

I also found a neat function the MS compiler uses called _gcvt

double number = 3123.21
char buffer[14]; //array of characters to hold the double

_gcvt( number, 14, buffer );

//_gcvt( doubleName, numberPrecision, characterArray );

cout << buffer;
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: AgentZap
Yep, got it now.

I also found a neat function the MS compiler uses called _gcvt

double number = 3123.21
char buffer[14]; //array of characters to hold the double

_gcvt( number, 14, buffer );

//_gcvt( doubleName, numberPrecision, characterArray );

cout << buffer;

Interesting - I never heard of that. It's available on linux/gcc as well, though without the underscore:

char *gcvt(double number, size_t ndigit, char *buf);
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
That's funny. Anyone ever heard of a game for NES called Adventures of Lolo? When I was like 8 years old my friend got to the last level and I memorized the password - it was GCVT. I will never be able to forget it.