Methods for converting numbers to strings in C language

Source: Internet
Author: User

The C language provides several standard library functions that convert numbers of any type (integer, long, float, and so on) to a string. The following is an example of converting an integer to a string using the Itoa () function:

# include <stdio. H>
# include <stdlib. H>
void main (void);
void Main (void)
{
int num = 100;
Char str[25];
ITOA (num, str, 10);
printf ("The number ' num ' is%d and the string ' str ' is%s. \ n",
num, str);
}

The itoa () function has 3 parameters: The first argument is the number to be converted, the second argument is the target string to write the result of the conversion, and the third argument is the cardinality used to transfer the number. In the example above, the conversion cardinality is 10.

The following functions can convert integers to strings:
----------------------------------------------------------
Function name action
----------------------------------------------------------
Itoa () converts an integer value to a string
Itoa () Converts a long integer value to a string
Ultoa () converts an unsigned long value to a string
----------------------------------------------------------
Note that the above function is incompatible with the ANSI standard. The ability to convert integers to strings and to be compatible with ANSI standards is to use the sprintf () function, see the following example:
#include <stdio.h>
# include <stdlib. H>

void main (void);
void Main (void)
{
int num = 100;
Char str[25];
sprintf (str, "%d", num);
printf ("The number ' num ' is%d and the string ' str ' is%s. \ n",
num, str);
}

When you convert a floating-point number to a string, you need to use a different set of functions. The following is an example of converting a floating-point value to a string using the FCVT () function:

# include <stdio. H>
# include <stdlib. H>
void main (void);
void Main (void)
{
Double num = 12345.678;
char * SIR;
int DEC_PL, sign, ndigits = 3; /* Keep 3 digits of precision. * /
str = FCVT (num, ndigits, &AMP;DEC-PL, &sign); /* Convert the float
to a string. * /
printf ("Original number;  %f\n ", num); /* Print the original
Floating-point
Value. * /
printf ("converted string;    %s\n ", str); /* Print the converted
String ' s value. * /
printf ("Decimal Place:%d\n", DEC-PI); /* Print the location of
The decimal point. * /
printf ("Sign:%d\n", sign); /* Print the sign.
0 = positive,
1 = negative. * /
}

The FCVT () function and the itoa () function have several significant differences. The FCVT () function has 4 parameters: the first parameter is the floating-point value to be converted, the second argument is the number of digits to the right of the decimal point in the conversion result, and the third argument is a pointer to an integer that returns the position of the decimal point in the conversion result, and the fourth argument is a pointer to an integer. The integer used to return the symbol for the result of the conversion (0 corresponds to a positive value and 1 corresponds to a negative value).

It is important to note that the FCVT () function does not really contain decimal decimal points in the conversion result, and for this reason, the FCVT () function returns where the decimal point should occupy in the conversion result. In the above example, the result value of the integer variable DEC_PL is 5 because the decimal point in the conversion result should be behind the 5th bit. If you require that the conversion result contain decimal points, you can use the GCVT () function (see table below).

The following function converts a floating-point value to a string:
-------------------------------------------------------------------------
Function name action
-------------------------------------------------------------------------
ECVT () converts a double-precision floating-point value to a string that does not contain a decimal point in the conversion result
FCVT () with the specified number of digits as the conversion precision, Yu Tong ecvt ()
GCVT () converts a double-precision floating-point value to a string that contains a decimal point in the conversion result

Methods for converting numbers to strings in C language

Related Article

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.