How to Convert Floating Point Number to String in DLL

Last reviewed: July 17, 1997
Article ID: Q88266
1.00 1.50 WINDOWS kbprg kbcode

The information in this article applies to:

  • The C Run-time (CRT), included with: Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SUMMARY

You 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 INFORMATION

The 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
KBCategory: kbprg kbcode
KBSubcategory: CRTIss
Keywords : CRTIss kb16bitonly kbcode kbprg
Version : 1.00 1.50
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.