home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:16530 comp.lang.c++:16289
- Newsgroups: comp.lang.c,comp.lang.c++
- Path: sparky!uunet!ukma!darwin.sura.net!spool.mu.edu!agate!iat.holonet.net!rkinder
- From: rkinder@iat.holonet.net (Robert J. Kinder)
- Subject: Re: Converting an int or a float into a string*???
- Message-ID: <Bxpywu.DM3@iat.holonet.net>
- Organization: HoloNet (BBS: 510-704-1058)
- References: <1992Nov13.135058.5330@magnus.acs.ohio-state.edu>
- Date: Sat, 14 Nov 1992 18:48:29 GMT
- Lines: 41
-
- yhtao@magnus.acs.ohio-state.edu (Yu-Hui Tao) writes:
- : Hi, netters:
- :
- : I have an array of numerical numbers of type either int or float.
- : What I want is to convert this array into of type char*. For example,
- : converting num[0]=1.02 into num[0]="1.02".
- :
- : How do I do this in C and C++? Hope this is not too trival!
- :
- :
- : Thanks!
- :
- : Yu-Hui Tao
- : ISE, OSU
- : tao@CSEL.eng.ohio-state.edu
- : ===========================
-
- It's easy but you won't find it looking at the atof() -Ascii To Float-
- function. Use sprintf(). Here's how:
-
- #include <stdio.h>
-
- main()
- {
- char str_num[25];
- float num;
- num = 456.328;
-
- sprintf( str_num, "%5.3f", num );
-
- /* str_num now has "456.328".
- * sprintf() uses the same format specifiers as printf().
- * Remember to make sure the char [] array is large enough to hold
- * the printed ascii result plus the null terminator '\0'.
- */
- }
- --
- // rkinder@holonet.net International Software Solutions, Inc. -
- // Robert J. Kinder, Jr. Boca Raton, Florida -
- // 1-800-788-4774 -
- // "We're having a real Ted Bundy day here." - C. Bernholtz
-