How to Convert Floating Point Number to String in DLLLast reviewed: July 17, 1997Article ID: Q88266 |
|
1.00 1.50
WINDOWS
kbprg kbcode
The information in this article applies to:
SUMMARYYou may want to convert a floating point number to a string in a dynamic link library (DLL) built with Microsoft Visual C++ version 1.0. The functions available to handle float-to-string conversion in a DLL are the C run-time functions _ecvt() and _fcvt(), and will work correctly in code built with all memory models.
MORE INFORMATIONThe sample code below demonstrates how to use _ecvt() and _fcvt() to convert a float to a string. These two functions work almost identically. The only difference is that the count parameter for _ecvt() specifies the number of digits to convert in the entire number (both the whole and fractional parts of the number), whereas the count parameter of the _fcvt() function specifies the number of digits after the decimal place (not including the whole part of the number.) The functions _gcvt() and sprintf() are not available in the run-time libraries for a DLL.
Sample Code
/* Compile options needed:** Select Windows DLL for Program Type in the Project dialog box under ** the Options menu. This code works in all memory models. */ #include <windows.h> #include <stdlib.h>LPSTR far FloatToString(LPSTR buff,double r,int prec) { char *buffer;
LPSTR startbuffptr;
static int dec,sign; // must use static, don't assume SS==DS
int i;
startbuffptr = buff;
buffer = fcvt((double)r,prec,&dec,&sign);
// buffer = ecvt((double)r,prec,&dec,&sign);
// copy the negative sign if less than zero
if (sign)
*buff++ = '-';
// copy the non-fractional part before the decimal place
for(i=0; i<dec; i++)
*buff++ = *buffer++;
*buff++ = '.'; // copy the decimal point into the string
*buff = '\0'; // don't assume NULL termination
// copy the fractional part after the decimal place
lstrcat(buff,buffer);
return startbuffptr;
}
Module Definition File (.DEF)LIBRARY FLOAT DESCRIPTION 'FLOAT.DLL -- a dll using ecvt() and fcvt()' EXETYPE WINDOWS STUB 'WINSTUB.EXE' CODE PRELOAD MOVEABLE NONDISCARDABLE DATA PRELOAD MOVEABLE SINGLE HEAPSIZE 1024 EXPORTS WEP @1 RESIDENTNAME
_FloatToString @2
|
Additional reference words: kbinf 1.00 1.50
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |