Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fcvt - Convert a floating-point number to a string

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

char *fcvt(double value, int n, int *dec_pt, int *sign);

fcvt converts a floating-point value to a string of digits and returns a pointer to this string as the result. The output format corresponds to the FORTRAN F format.

The string begins with the first non-zero digit of the floating-point number to be converted and includes n decimal places.

The decimal point and a negative sign, if any, do not form a part of the string. However, fcvt returns the position of the decimal point and the sign in result parameters.

Parameters

double value

Floating-point value that is to be edited for output.

int n

Number of digits after the decimal point.

If n is less than the number of digits after the decimal point in value, the least significant digit is rounded (as in the FORTRAN F format).
If n is greater, zero padding is used for right justification.

int *dec_pt

Pointer to an integer that specifies the position of the decimal point in the result string.

Positive number: position relative to the beginning of the result string.
Negative number or 0: the decimal point is to the left of the first digit.

int *sign

Pointer to an integer that specifies the sign of the result string.

0: the sign is positive
Not equal to 0: the sign is negative

Return val.

Pointer to the converted string. fcvt terminates the string with the null byte (\0).

Notes

Invalid parameters, e.g. an integer value instead of a double value, cause the program to abort!

Note that the arguments dec_pt and sign must be pointers!

fcvt writes its result into an internal C data area that is overwritten with each call! The ecvt function also uses the same data area.

Example

The following program reads a floating-point value x, converts it as specified in n according to the FORTRAN F format, and outputs it as a string. In addition, the calculated sign and the position of the decimal point dec_pt are output.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  double x;
  int n, dec_pt, sign;
  printf("Please enter floating-point number:  \n");
  if (scanf("%lf", &x) == 1)
  {
    printf("How many significant digits?:  \n");
    if (scanf("%d", &n) == 1)
    {
      printf("The converted number is :  %s \n",
             fcvt(x, n, &dec_pt, &sign));
      printf("The sign is %s \n",
             (sign == 0 ? "positive" : "negative"));
      printf("The position of the decimal point is: %d \n", dec_pt);
    }
  }
  return 0;
}

See also

ecvt, gcvt