home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16530 < prev    next >
Encoding:
Text File  |  1992-11-12  |  1.6 KB  |  53 lines

  1. Xref: sparky comp.lang.c:16530 comp.lang.c++:16289
  2. Newsgroups: comp.lang.c,comp.lang.c++
  3. Path: sparky!uunet!ukma!darwin.sura.net!spool.mu.edu!agate!iat.holonet.net!rkinder
  4. From: rkinder@iat.holonet.net (Robert J. Kinder)
  5. Subject: Re: Converting an int or a float into a string*???
  6. Message-ID: <Bxpywu.DM3@iat.holonet.net>
  7. Organization: HoloNet (BBS: 510-704-1058)
  8. References: <1992Nov13.135058.5330@magnus.acs.ohio-state.edu>
  9. Date: Sat, 14 Nov 1992 18:48:29 GMT
  10. Lines: 41
  11.  
  12. yhtao@magnus.acs.ohio-state.edu (Yu-Hui Tao) writes:
  13. : Hi, netters:
  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. :    Thanks!
  19. : Yu-Hui Tao
  20. : ISE, OSU
  21. : tao@CSEL.eng.ohio-state.edu
  22. : ===========================
  23.  
  24. It's easy but you won't find it looking at the atof() -Ascii To Float-
  25. function.  Use sprintf().  Here's how:
  26.  
  27. #include <stdio.h>
  28.  
  29. main()
  30. {
  31.    char  str_num[25];
  32.    float num;
  33.    num = 456.328;
  34.  
  35.    sprintf( str_num, "%5.3f", num );
  36.  
  37.    /* str_num now has "456.328".  
  38.     * sprintf() uses the same format specifiers as printf().
  39.     * Remember to make sure the char [] array is large enough to hold
  40.     * the printed ascii result plus the null terminator '\0'.
  41.     */
  42. }
  43. -- 
  44. // rkinder@holonet.net            International Software Solutions, Inc.  -
  45. // Robert J. Kinder, Jr.                             Boca Raton, Florida  -
  46. //                                                        1-800-788-4774  -
  47. // "We're having a real Ted Bundy day here." - C. Bernholtz
  48.