Syntax:
It has the following syntax:
Example
char * fcvt (double val, int n, int * dece, int * signs);
- double val: The floating-point value will be converted to a string.
- int n: It is the number of digits that the function will return. If it is larger than the total amount of digits in value, the remainder of the string is padded with zeros; otherwise, the low-order digit is reduced.
- int * dece: It is an integer pointer that stores the decimal point location relative to the beginning of the text. If it is 0 or less than zero, the decimal point is located just to the left of the digits.
- int * signs: It is an integer pointer that gets the sign indication, where 0 indicates a positive sign, and non-zero indicates a negative sign.
The function outputs a null-terminated string of characters with the same length as the specified number. This string will hold the individual digits of the double precision number given as input.
The upcoming C code examples demonstrate the utilization of the fcvt function:
Example 1:
Example
//Program to implement the fctv() function in C
#include <stdio.h>
#include <stdlib.h>
// Function for finding the result
void useOfFcvt()
{
double x1 = 128.7654;
char* buffer;
int dece, signs;
// the function fctv() is called
buffer = fcvt(x1, 6, &dece, &signs);
// display of the result string after conversion
printf("The value of the string is: %c%c.%sX10^%d\n",
signs == 0 ? '+' : '-',
'0', buffer, dece);
}
// main
int main()
{
// function calling
useOfFcvt();
return 0;
}
Output:
Output
The value of the string is: +0.128765400X10^3
Example 2:
Example
//Program to implement the fctv() function in C
#include <stdio.h>
#include <stdlib.h>
// main
int main(void)
{
char* str;
double val;
int Dece, signs;
int ndigs = 10;
val = -8.676;
str= fcvt(val, ndigs,
&Dece, &signs);
printf("The converted string"
" value is: %s Dec "
"is: %d sign is: %d\n",
str, Dece, signs);
return 0;
}
Output:
Output
The converted string value is: 86760000000 Dec is: 1 sign is: 1
Advantages of using fctv
There are several advantages of the fcvt function in C. Some main advantages of the fcvt function are as follows:
- This function allows developers to convert floating-point integers into strings with many formatting choices, such as decimal precision, scientific notation, and the representation of unexpected numbers such as infinity or NaN (Not a Number) . It supports many formatting options, allowing developers to select between various styles and accuracy levels.
- For example, you may use the fcvt function to convert a floating-point integer to a string in either fixed-point or scientific notation by specifying the number of decimal places, the resultant string's minimum width, and whether or not you want to pad with zeros.
- The fcvt function enables the customization of output representations of floating-point numbers by providing this degree of control, assuring precision and desired format when presenting or storing these numbers in different programs or systems.