home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16716 < prev    next >
Encoding:
Text File  |  1992-11-18  |  1.5 KB  |  40 lines

  1. Xref: sparky comp.lang.c:16716 comp.lang.c++:16498
  2. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!uwm.edu!linac!uchinews!machine!ddsw1!infopls!rhps!warlok
  3. From: warlok@rhps.chi.il.us (Jon L Fincher)
  4. Newsgroups: comp.lang.c,comp.lang.c++
  5. Subject: Re: Converting an int or a float into a string*???
  6. Message-ID: <mT1cuB12w165w@rhps.chi.il.us>
  7. Date: Mon, 16 Nov 92 19:32:57 CST
  8. References: <1992Nov13.135058.5330@magnus.acs.ohio-state.edu>
  9. Organization: CrissySoft Incorporated
  10. Lines: 28
  11.  
  12. yhtao@magnus.acs.ohio-state.edu (Yu-Hui Tao) writes:
  13.  
  14. >    I have an array of numerical numbers of type either int or float. 
  15. > What I want is to convert this array into of type char*. For example, 
  16. > converting num[0]=1.02 into num[0]="1.02".   
  17. >    How do I do this in C and C++? Hope this is not too trival!
  18.  
  19. You could try to standard C function fcvt(), which tranlates a float to
  20. a string.  Cast your ints to doubles, then call fcvt(), syntax:
  21.  
  22.   char *fcvt( double val, int ndig, int * dec, int *sign);
  23.  
  24. val - the number to convert.
  25. ndig - length of string to contain translation.
  26. dec - pointer to a position in the string where the decimal
  27.       point goes.
  28. sign - points to non-zero value if val is negative, 0 otherwise.
  29.  
  30. The return value is static data, so strcpy() it before you call fcvt()
  31. again.  Note that dec and sign are returned values - there is no
  32. decimal point in the string and no sign value.  You can construct a
  33. signed, pointed number using strcat() and strcpy() just after you call fcvt().
  34.  
  35. Hope you can handle that yourself....
  36.  
  37. J.L.Fincher,  Warlok
  38.  
  39.