gcvt
char *  gcvt ( double value, int num, char * buffer );
stdlib.h
  cplusplus.com  

Convert floating point value to string.
  Converts value to a string of num digits plus a terminating null character. gcvt tries to generate a string with just the number in decimal including a decimal point and, optionally, a negative sign plus. If num digits is insufficient to express the value that way the function will generate a number in exponential form just like the printf %e option does. The function also appends a null-character at the end.
So the buffer required for the resulting string is usually larger than num characters.

Parameters.

value
Floating point value to be converted to a string.
num
Number of digits to generate.
buffer
Memory block where to store the resulting string. This should be some characters larger than the num parameter in order to store a decimal point, a sign, maybe exponential information, and an ending null-character. In total, depending on the implementation, buffer should be between 8 and 9 characters larger than num to contain any possible result.

Return Value.
  A pointer to the null-terminated string of digits.

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

Example.

/* gcvt example */
#include <stdio.h>
#include <stdlib.h>

main ()
{
  char buffer [20];
  gcvt (1365.249,6,buffer);
  puts (buffer);
  gcvt (1365.249,3,buffer);
  puts (buffer);
  return 0;
}
Output:
1365.25
1.37e+003

See also.
  ecvt, fcvt, atof


© The C++ Resources Network, 2000