fcvt
char *  fcvt ( double value, int num, int * dec, int * sign );
stdlib.h
  cplusplus.com  

Convert floating point value to string.
  Converts the value to a null-terminated string of num digits. If num is greater than the representable digits of value the rest of the string is padded with zeros. If num is smaller the low-order digit is rounded.
  Only digits are returned. The position of the decimal point can be otained from dec, that is a pointer to an int value representing the position of the decimal point respect to the beginning of the string (0 or less indicates that the decimal point lies to the left of the digits). The sign can be obtained from the sign parameter, that points to an int value: if this is 0, the value is positive, otherwise it is negative.
  Actually acts exactly as ectv.

Parameters.

value
Floating point value to be converted to a string.
num
Number of digits to be returned. If this is greater than the number of representable digits the rest of the string is padded with zeros. If this is smaller the low-order digit is rounded.
dec
Pointer to an int where to store the decimal-point position respect to the beginning of the string. 0 or less indicates that the decimal point lies to the left of the digits.
sign
Pointer to an int that receives the sign indicator: 0 means positive sign, non-zero means negative.

Return Value.
  A null-terminated buffer with the legth specified by num that contains the digits of the value. Decimal point and sign are not returned in the buffer itself: see dec and sign parameters.
  The buffer returned is a block of memory statically allocated and will be overwritten by further calls to this function.

Portability.
  Not defined in ANSI-C, but included in some compilers

Example.

/* fcvt example: scientific notations */
#include <stdio.h>
#include <stdlib.h>

main ()
{
  char *buffer;
  double value = 365.249;
  int precision = 5;
  int decimal, sign;
  
  buffer = fcvt (value, precision, &decimal, &sign);
  printf ("%c%c.%s x 10^%d\n",sign?'-':'+',buffer[0],buffer+1,decimal-1);
  return 0;
}
Output:
+3.6525 x 10^2

See also.
  atof, gcvt


© The C++ Resources Network, 2000