Microsoft KB Archive/57244

= INFO: Workaround for Converting a Float/Double to a String =

Article ID: 57244

Article Last Modified on 12/11/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft C/C++ Professional Development System 7.0

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q57244

<div class="notice_section">

<div class="summary_section">

SUMMARY
The gcvt function returns an exponential number in the string even if the number fits in the specified precision when the number is of the form 0.0x, where x is any digit(s). Because of this behavior with the gcvt function, floating-point numbers cannot be converted to a string. Another function that converts a floating-point number to a string is fcvt. Unfortunately, fcvt does not do all the conversion for you because it leaves out both the decimal point and the sign of the number.

You can also use the sprintf or printf function with the &quot;%lf&quot; format specifier to obtain the correct results. However, if you do not want to use any printf constructs, supporting code is needed to completely convert the floating-point number to a string. The following program shows one possible way this can be done, and the printf statements can be replaced by puts statements:

Sample Code

 * 1) include <stdio.h>
 * 2) include <stdlib.h>
 * 3) include <malloc.h>
 * 4) include <string.h>


 * 1) define PRECISION  8

char *double_to_char (double) ;

char *temp2 ;

void main (void) {  char buf[32];

temp2 = double_to_char ((double) 0.0004567891) ; printf (&quot;temp = %s\n&quot;, temp2) ; free (temp2) ;

gcvt ((double) 0.0004567891, PRECISION, buf) ; printf (&quot;temp = %s\n&quot;, buf) ;

temp2 = double_to_char ((double) 123.564) ; printf (&quot;temp = %s\n&quot;, temp2) ; free (temp2) ;

temp2 = double_to_char ((double) -43.7864383846738) ; printf (&quot;temp = %s&quot;, temp2) ; free (temp2) ; }

/* Translates a double to an ASCIIZ string. */

char *double_to_char (double number) {  char *buffer, *temp ;

int decimal_spot, sign, count, current_location = 0 ;

temp = fcvt (number, PRECISION, &decimal_spot, &sign) ;

if (strlen (temp) > PRECISION) buffer = (char *) malloc (strlen (temp) + 3) ; else buffer = (char *) malloc (PRECISION + 3) ;

if (buffer == NULL) {     printf (&quot;Memory allocating attempt has failed in&quot;              &quot;'double_to_char'\n&quot;) ; exit (-1) ; }

/* Add negative sign if required. */

if (sign) buffer [current_location++] = '-' ;

/* Place decimal point in the correct location. */

if (decimal_spot > 0) {     strncpy (&buffer [current_location], temp, decimal_spot) ; buffer [decimal_spot + current_location] = '.' ; strcpy (&buffer [decimal_spot + current_location + 1],                     &temp [decimal_spot]) ; }  else {     buffer [current_location] = '.' ; for(count = current_location;            count<abs(decimal_spot)+current_location; count++) buffer [count + 1] = '0' ; strcpy (&buffer [count + 1], temp) ; }

return (buffer) ; }

Keywords: kbinfo kbcrt KB57244

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.