Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: Converting double to CString and back

0 views
Skip to first unread message

Joel Shellman

unread,
Sep 13, 1996, 3:00:00 AM9/13/96
to

Sorry to ask such a trivial question, but I can't believe there's no
simple way to convert a double to a CString and vice versa. I already
looked through the help and couldn't find anything (it being in Japanese
doesn't help but...)

If anyone can help, i'd appreciate it.

Thanks,

joel
shel...@softopia.pref.gifu.jp

Robert A. Wolf III

unread,
Sep 13, 1996, 3:00:00 AM9/13/96
to

Here it is Joel:

Converts a floating-point value to a string, which it stores in a
buffer.
char *_gcvt( double value, int digits, char *buffer );


Routine Required Header Optional Headers Compatibility

_gcvt <stdlib.h> Win 95, Win NT, Win32s, 68K, PMac
For additional compatibility information, see Compatibility in the
Introduction.

Libraries


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRTx0.DLL, retail version
MSVCRTx0.DLL Multithread DLL library, retail version
Return Value
_gcvt returns a pointer to the string of digits. There is no error
return.
Parameters
value Value to be converted
digits Number of significant digits stored
buffer Storage location for result
Remarks
The _gcvt function converts a floating-point value to a character string
(which includes a decimal point and a possible sign byte) and stores the
string in buffer. The buffer should be large enough to accommodate the
converted value plus a terminating null character, which is appended
automatically. If a buffer size of digits + 1 is used, the function
overwrites the end of the buffer. This is because the converted string
includes a decimal point and can contain sign and exponent information.
There is no provision for overflow. _gcvt attempts to produce digits
digits in decimal format. If it cannot, it produces digits digits in
exponential format. Trailing zeros may be suppressed in the conversion.
Example

/* _GCVT.C: This program converts -3.1415e5
* to its string representation.
*/

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char buffer[50];
double source = -3.1415e5;
_gcvt( source, 7, buffer );
printf( "source: %f buffer: '%s'\n", source, buffer );
_gcvt( source, 7, buffer );
printf( "source: %e buffer: '%s'\n", source, buffer );
}


Output

source: -314150.000000 buffer: '-314150.'
source: -3.141500e+005 buffer: '-314150.'

good luck,
Robert

Joel Shellman

unread,
Sep 17, 1996, 3:00:00 AM9/17/96
to

Robert A. Wolf III wrote:
>
> > Sorry to ask such a trivial question, but I can't believe there's no
> > simple way to convert a double to a CString and vice versa. I already
> > looked through the help and couldn't find anything (it being in Japanese
> > doesn't help but...)
>
> Here it is Joel:
> Converts a floating-point value to a string, which it stores in a
> buffer.
> char *_gcvt( double value, int digits, char *buffer );

Thanks for your reply. I should have mentioned that I already knew of
gcvt(). That's why I said a CString as opposed to just any string. I'm
already implemeting it as you suggested--it just seems to be absurd to
mix all this nice MFC programming with gcvt() which uses an entirely
different library and system.

Yes, I'm one of those lost programmers who thinks he can still write
clean, nice code.

Thanks anyway,

-joel

William E. Kempf

unread,
Sep 19, 1996, 3:00:00 AM9/19/96
to

Joel Shellman <shel...@softopia.pref.gifu.jp> wrote:

The Format member function should convert to a CString. As for
converting in the reverse direction... create a CStringExt class with
a conversion function (using one of the many standard conversion
functions). In this way you only code it once, and have a simple
"clean" way of doing it. Don't lock yourself in a C mentality... you
are in C++ so encapsulate everywhere you find functionality lacking!

-----
William E. Kempf : http://www.novia.net/~srwillrd
"Sir Willard" : mailto:srwi...@novia.net (home)
Knight of the Ascii Table :

0 new messages