[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

URGENT Please... (double to string)

Posted on 1999-10-08
16
Medium Priority
?
382 Views
Last Modified: 2010-04-15
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
Comment
Question by:skundu
  • 4
  • 3
  • 2
  • +5
16 Comments
 
LVL 85

Expert Comment

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

Expert Comment

by:AlexVirochovsky
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

by:abhayap20
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
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 

Author Comment

by:skundu
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

by:jhance
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

by:AlexVirochovsky
ID: 2113035
>>thanks, it worked!
Ok, wer are very glad to help you! And who can submit Question?
0
 

Expert Comment

by:Matthew85H
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

by:jhance
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

by:Matthew85H
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

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

Expert Comment

by:Matthew85H
ID: 2121382
hmm.... answer rejected?
0
 

Expert Comment

by:sa9813
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

by:rbr
ID: 2123419
Pls stop your comments. ozo has answered the question correct. Nothing more to say.
0
 

Accepted Solution

by:
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

by:skundu
ID: 2278973
thanks...
0
 

Expert Comment

by:abhayap20
ID: 2282363
u are always welcome..
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question