• 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
skunduAsked:
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.

ozoCommented:
mychar = malloc(DBL_MAX_10_EXP+DBL_DIG);
sprintf(mychar,"%.3f",mydouble);
0
AlexVirochovskyCommented:
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
abhayap20Commented:
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
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

skunduAuthor 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
jhanceCommented:
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
AlexVirochovskyCommented:
>>thanks, it worked!
Ok, wer are very glad to help you! And who can submit Question?
0
Matthew85HCommented:
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
jhanceCommented:
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
Matthew85HCommented:
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
Matthew85HCommented:
wow, I just read up on ur record, you probably have jeans older than me!!! I'm 14 :)
0
Matthew85HCommented:
hmm.... answer rejected?
0
sa9813Commented:
Maybe this is the function u are looking for

Syntax

#include <stdlib.h>
char *ecvt(double value, int ndig, int *dec, int *sign);
0
rbrCommented:
Pls stop your comments. ozo has answered the question correct. Nothing more to say.
0
abhayap20Commented:
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

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skunduAuthor Commented:
thanks...
0
abhayap20Commented:
u are always welcome..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.