parent nodes: CPlusPlusApuntesC
Data conversion
Conversión de datos
Author: César TM
Date: 20/07/2005
Source: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_data_conversion.asp
_ecvt Convert double to string of specified length
_fcvt Convert double to string with specified number of digits following decimal point
_gcvt Convert double number to string; store string in buffer
Estas conversiones no son estándar, afectan negativamente a la portabilidad, en lugar de eso es mejor usar sprintf con "%d" para el itoa o con "%2f" para el fcvt.
Está en la librería "stdlib.h"
_ecvt
Converts a double number to a string.char *_ecvt( double value, int count, int *dec, int *sign );
Parameters
value = Number to be converted
count = Number of digits stored
dec = Stored decimal-point position
sign = Sign of converted number
The _ecvt function converts a floating-point number to a character
string. The value parameter is the floating-point number to be
converted. This function stores up to count digits of value as a string and
appends a null character ('0'). If the number of digits in value exceeds
count, the low-order digit is rounded. If there are fewer than count digits,
the string is padded with zeros.
Only digits are stored in the string. The position of the decimal point and
the sign of value can be obtained from dec and sign after the call. The dec
parameter points to an integer value giving the position of the decimal point
with respect to the beginning of the string. A 0 or negative integer value
indicates that the decimal point lies to the left of the first digit. The
sign parameter points to an integer that indicates the sign of the converted
number. If the integer value is 0, the number is positive. Otherwise, the
number is negative. /* ECVT.C: This program uses _ecvt to convert a
* floating-point number to a character string.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int decimal, sign;
char *buffer;
int precision = 10;
double source = 3.1415926535;
buffer = _ecvt( source, precision, &decimal, &sign );
printf( "source: %2.10f buffer: '%s' decimal: %d sign: %d\n",
source, buffer, decimal, sign );
}
_fcvt
Converts a floating-point number to a string.char *_fcvt( double value, int count, int *dec, int *sign );
Parameters
value = Number to be converted
count = Number of digits after decimal point (!)
dec = Pointer to stored decimal-point position
sign = Pointer to stored sign indicator
The _fcvt function converts a floating-point number to a null-terminated
character string. The value parameter is the floating-point number to be
converted. _fcvt stores the digits of value as a string and appends a null
character ('0'). The count parameter specifies the number of digits to be
stored after the decimal point. Excess digits are rounded off to count
places. If there are fewer than count digits of precision, the string is
padded with zeros.
Only digits are stored in the string. The position of the decimal point and
the sign of value can be obtained from dec and sign after the call. The dec
parameter points to an integer value; this integer value gives the position
of the decimal point with respect to the beginning of the string. A zero or
negative integer value indicates that the decimal point lies to the left of
the first digit. The parameter sign points to an integer indicating the sign
of value. The integer is set to 0 if value is positive and is set to a
nonzero number if value is negative.
/* FCVT.C: This program converts the constant
* 3.1415926535 to a string and sets the pointer
* *buffer to point to that string.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int decimal, sign;
char *buffer;
double source = 3.1415926535;
buffer = _fcvt( source, 7, &decimal, &sign );
printf( "source: %2.10f buffer: '%s' decimal: %d sign: %d\n",
source, buffer, decimal, sign );
}
gcvt
Converts a floating-point value to a string, which it stores in a buffer.char *_gcvt( double value, int digits, char *buffer );
Parameters
value = Value to be converted
digits = Number of significant digits stored
buffer = Storage location for result
The _gcvt function converts a floating-point value to a character string
(which includes a decimal point and a possible sign byte) and stores the
string in buffer. The buffer should be large enough to accommodate the
converted value plus a terminating null character, which is appended
automatically. If a buffer size of digits + 1 is used, the function
overwrites the end of the buffer. This is because the converted string
includes a decimal point and can contain sign and exponent information. There
is no provision for overflow. _gcvt attempts to produce digits digits in
decimal format. If it cannot, it produces digits digits in exponential
format. Trailing zeros may be suppressed in the conversion.
/* _GCVT.C: This program converts -3.1415e5
* to its string representation.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char buffer[50];
double source = -3.1415e5;
_gcvt( source, 7, buffer );
printf( "source: %f buffer: '%s'\n", source, buffer );
_gcvt( source, 7, buffer );
printf( "source: %e buffer: '%s'\n", source, buffer );
}
Output
source: -314150.000000 buffer: '-314150.'
source: -3.141500e+005 buffer: '-314150.'
__________________________________________________________________________________________________________
CPlusPlusApuntesC [camelCaseWordsEnabled: false]