Contrastive analysis of GCVT () and ECVT () and FCVT () function _c language in C language

Source: Internet
Author: User

GCVT () function:
header files: #include <stdlib.h>

To define a function:

Char *GCVT (double number, size_t ndigits, Char *buf);

Function Description: GCVT () is used to convert the parameter number to an ASCII string, and the parameter ndigits indicates how many digits are displayed. The difference between GCVT () and ECVT () and FCVT () is that the converted string in GCVT () contains a decimal point or a positive or negative symbol. If the conversion succeeds, the converted string is placed in the space indicated by the parameter buf pointer.

Return value: Returns a string pointer, which is the BUF pointer.

Example

#include <stdlib.h>
Main () {
  double A = 123.45;
  Double b = -1234.56;
  char *ptr;
  int decpt, sign;
  GCVT (A, 5, PTR);
  printf ("A value=%s\n", PTR);
  ptr = GCVT (b, 6, PTR);
  printf ("b value=%s\n", PTR);
}

Execution results:

A value=123.45
b value=-1234.56

ECVT () function:

Function: Converts a double-precision floating-point number to a string and does not include a decimal point in the conversion result.

Usage

Char *ecvt (double value, int ndigit, int *decpt, int *sign);

Detailed explanation: The ECVT function converts a double-precision floating-point number into a string. The value parameter is the floating-point number to convert. This function stores up to ndigit numeric values as a string and adds an empty number character (' "), and if the number in value exceeds Ndigit, the low number is rounded. If the number is less than ndigit, the string is padded with 0.

Only numbers are stored in the string, and the decimal position and value symbol are fetched from DECPT and sign after the call. The DECPT parameter indicates the integer value given to the position of the decimal point, which is computed from the beginning position of the string. 0 or negative numbers indicate that the decimal point is to the left of the first digit. The sign parameter indicates an integer that indicates the symbol of the converted number. If the integer is 0, the number is positive, otherwise it is negative.

Parameters

    • Value: The double-precision floating-point number to be converted.
    • Ndigit: The number of significant digits stored.
    • *DECPT: The location of the decimal point to store.
    • *sign: The symbol of the converted number.

return value:

    • Char*: Points to the generated string.

Note: The header file for this function is "stdlib.h".


fcvt () function

Function Name: FCVT

Function: Converts a floating-point number to a string

Header file:

#include <stdlib.h>

Prototype

*FCVT (double value, int ndigit, int *decpt, int *sign);

Parameter Description: Value is the floating-point number to convert, Ndigit is the digits after the decimal point, *decpt represents the position of the decimal point, *sign symbol, 0 is positive, and 1 is negative.

Instance code:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
  
int main (void)
{
  char *string;
  Double value;
  int Dec, sign;
  int ndig = ten;
  CLRSCR ();
  Value = 9.876;
  String = FCVT (value, Ndig, &dec, &sign);
  printf ("string =%s Dec =%d sign =%d\n", string, Dec, sign);
  Value = -123.45;
  ndig=;
  String = FCVT (value,ndig,&dec,&sign);
  printf ("string =%s Dec =%d sign =%d\n", string, Dec, sign);
  value = 0.6789e5; /* Scientific notation * *
  ndig = 5;
  String = FCVT (value,ndig,&dec,&sign);
  printf ("string =%s Dec =%d sign =%d\n", string, Dec, sign);
  return 0;
}

The output results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.