• C

# URGENT Please... (double to string)

How can i convert a double variable to a string??
double mydouble= 5.678;
char *mychar;
I want to get mychar containging "5.678"
Pls help...
sunanda
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
mychar = malloc(DBL_MAX_10_EXP+DBL_DIG);
sprintf(mychar,"%.3f",mydouble);
0
Commented:
Or can use fcvt function:

/* fcvt example */

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char *str;
double num;
int dec, sign, ndig = 5;

/* a regular number */
num = 9.876;
str = fcvt(num, ndig, &dec, &sign);
printf("string = %10s decimal place = %d sign = %d\n", str, dec, sign);

/* a negative number */
num = -123.45;
str = fcvt(num, ndig, &dec, &sign);
printf("string = %10s decimal place = %d sign = %d\n", str, dec, sign);

/* scientific notation */
num = 0.678e5;

str = fcvt(num, ndig, &dec, &sign);
printf("string = %10s  decimal place= %d  sign = %d\n", str, dec, sign);
return 0;
}
Alex
0
Commented:
hi sunanda,

u can convert double to string using inbuilt function in turbo c++ named as 'gcvt'

using this  will solve ur problem

bye,
abhay
0
Author Commented:
hi abhay,

i do not know how to use 'gcvt' function. but thanks for helping me.
sunanda

hi ozo, alex,
thanks, it worked!
0
Commented:
This is the easiest...

>mychar = malloc(DBL_MAX_10_EXP+DBL_DIG);
>sprintf(mychar,"%.3f",mydouble);

But, don't forget the:

free(mychar);

or you'll leak memory!
0
Commented:
>>thanks, it worked!
0
Commented:
Okay
mychar= (char) mydouble;
that's it!!! Yeah, it works, I mean it, it works, try it, it can't fail if it does hmm... I guess change char to double? but I don't think so.
I think that works, another way is memory copying.
0
Commented:
Matthew!!! Where have you been all my career!  Man, I've been doing things that hard way for years, and now you come up with this BREAKTHROUGH!!!!  Man, you're a genius!
0
Commented:
Well, where have I been? Gee, a GREAT friend of mine Gladiator or better known here as Viktornet (#12 or 13 in C and C++, #4 in Delphi and a # something in VB) told me about this site pretty long time ago, I KEPT NOT COMMING HERE CUZ I DIDN'T KNOW WHAT IT WAS. So today I read up on his resume and make fun of him :) and I found this site AGAIN, so I said why not. And since today I've been answering all these questions from C and C++ :) well, that's all I gotta say about that. I'll see if you I can help you guys more.... not that I don't need help. ICQ me at 13010590 if you wanna. Visit my site http://fly.to/sflare
0
Commented:
wow, I just read up on ur record, you probably have jeans older than me!!! I'm 14 :)
0
Commented:
0
Commented:
Maybe this is the function u are looking for

Syntax

#include <stdlib.h>
char *ecvt(double value, int ndig, int *dec, int *sign);
0
Commented:
0
Commented:
the gcvt function is as follow ==>

gcvt( double num, int dig, char * str)

here num is ur actual number to be converted into string.
dig is the numbers of floating points
while str is the string into which u have to change ur no.

in ur case it will be

gcvt(mydouble,3,mychar)

isn't it simple???

bye,
abhay.
0

Experts Exchange Solution brought to you by