Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# URGENT Please... (double to string)

Posted on 1999-10-08
Medium Priority
380 Views
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
0
Question by:skundu
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +5

LVL 84

Expert Comment

ID: 2112285
mychar = malloc(DBL_MAX_10_EXP+DBL_DIG);
sprintf(mychar,"%.3f",mydouble);
0

LVL 14

Expert Comment

ID: 2112309
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

Expert Comment

ID: 2112671
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 Comment

ID: 2112934
hi abhay,

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

hi ozo, alex,
thanks, it worked!
0

LVL 32

Expert Comment

ID: 2112951
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

LVL 14

Expert Comment

ID: 2113035
>>thanks, it worked!
0

Expert Comment

ID: 2117960
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

LVL 32

Expert Comment

ID: 2118065
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

Expert Comment

ID: 2118367
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

Expert Comment

ID: 2118370
wow, I just read up on ur record, you probably have jeans older than me!!! I'm 14 :)
0

Expert Comment

ID: 2121382
0

Expert Comment

ID: 2123107
Maybe this is the function u are looking for

Syntax

#include <stdlib.h>
char *ecvt(double value, int ndig, int *dec, int *sign);
0

LVL 10

Expert Comment

ID: 2123419
0

Accepted Solution

abhayap20 earned 150 total points
ID: 2123875
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

Author Comment

ID: 2278973
thanks...
0

Expert Comment

ID: 2282363
u are always welcome..
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month9 days, 21 hours left to enroll