home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:16716 comp.lang.c++:16498
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!uwm.edu!linac!uchinews!machine!ddsw1!infopls!rhps!warlok
- From: warlok@rhps.chi.il.us (Jon L Fincher)
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: Converting an int or a float into a string*???
- Message-ID: <mT1cuB12w165w@rhps.chi.il.us>
- Date: Mon, 16 Nov 92 19:32:57 CST
- References: <1992Nov13.135058.5330@magnus.acs.ohio-state.edu>
- Organization: CrissySoft Incorporated
- Lines: 28
-
- yhtao@magnus.acs.ohio-state.edu (Yu-Hui Tao) writes:
-
- > 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!
-
- You could try to standard C function fcvt(), which tranlates a float to
- a string. Cast your ints to doubles, then call fcvt(), syntax:
-
- char *fcvt( double val, int ndig, int * dec, int *sign);
-
- val - the number to convert.
- ndig - length of string to contain translation.
- dec - pointer to a position in the string where the decimal
- point goes.
- sign - points to non-zero value if val is negative, 0 otherwise.
-
- The return value is static data, so strcpy() it before you call fcvt()
- again. Note that dec and sign are returned values - there is no
- decimal point in the string and no sign value. You can construct a
- signed, pointed number using strcat() and strcpy() just after you call fcvt().
-
- Hope you can handle that yourself....
-
- J.L.Fincher, Warlok
-
-