Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

ecvt - Convert a floating-point number to a string

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

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

ecvt converts a floating-point number value to a string of n digits and returns a pointer to this string as its result.

The string begins with the first non-zero digit of the floating-point number, i.e. leading zeros are not included.

The decimal point and a negative sign, if any, do not form a part of the string. However, ecvt 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 in the result string (calculated from the first non-zero digit of the floating-point number to be converted).

If n is less than the number of digits in value, the least significant digit is rounded.
If n is greater, zero padding is used for right justification.

int *dec_pt

Pointer to an integer specifying 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 specifying 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.

ecvt terminates the string with the null byte (\0).

Notes

An invalid parameter, such as an integer value instead of a double value, causes the program to abort!

Note that the arguments dec_pt and sign must be pointers!

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

Example

The following program reads a floating-point value x, converts it as specified in n, 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;
  char *s;
  printf("Please enter floating-point number:  \n");
  if (scanf("%lf", &x) == 1)
  {
    printf("How many significant digits?:  \n");
    if (scanf("%d", &n) == 1)
    {
      s = ecvt(x, n, &dec_pt, &sign);
      printf("The string is: %s\n", s);
      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

fcvt, gcvt