_gcvt_gcvt

将浮点值转换为字符串,并将其存储在缓冲区中。Converts a floating-point value to a string, which it stores in a buffer. 此函数有一个更安全的版本;请参阅 _gcvt_sA more secure version of this function is available; see _gcvt_s.

语法Syntax

char *_gcvt(
   double value,
   int digits,
   char *buffer
);

参数Parameters

valuevalue
要转换的值。Value to be converted.

digitsdigits
存储的有效位数。Number of significant digits stored.

bufferbuffer
结果的存储位置。Storage location for the result.

返回值Return Value

_gcvt将指针返回到数字的字符串。_gcvt returns a pointer to the string of digits.

备注Remarks

_Gcvt函数将转换浮点为字符字符串 (其中包括小数点和可能的登录字节),并将存储中的字符串缓冲区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. 缓冲区应足够大,以适应转换后的值加上会自动追加终止 null 字符。The buffer should be large enough to accommodate the converted value plus a terminating null character, which is appended automatically. 如果缓冲区大小为数字+ 1 时,该函数将覆盖缓冲区末尾。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尝试生成数字以十进制格式的数字。_gcvt attempts to produce digits digits in decimal format. 如果无法实现,它会生成数字指数格式的数字。If it cannot, it produces digits digits in exponential format. 在转换过程中,可以取消零结尾。Trailing zeros might be suppressed in the conversion.

A缓冲区的长度 _CVTBUFSIZE足以满足任何浮点值。A buffer of length _CVTBUFSIZE is sufficient for any floating point value.

此函数验证其参数。This function validates its parameters. 如果缓冲区NULL,则调用无效参数处理程序,如中所述参数验证If buffer is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,此函数将errnoEINVAL并返回NULLIf execution is allowed to continue, this function sets errno to EINVAL and returns NULL.

要求Requirements

例程Routine 必需的标头Required header
_gcvt_gcvt <stdlib.h><stdlib.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

// crt_gcvt.c
// compile with: /W3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( void )
{
   char buffer[_CVTBUFSIZE];
   double value = -1234567890.123;
   printf( "The following numbers were converted by _gcvt(value,12,buffer):\n" );
   _gcvt( value, 12, buffer ); // C4996
   // Note: _gcvt is deprecated; consider using _gcvt_s instead
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value *= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value *= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value *= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );

   printf( "\n" );
   value = -12.34567890123;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value /= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value /= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
   value /= 10;
   _gcvt( value, 12, buffer ); // C4996
   printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
}
The following numbers were converted by _gcvt(value,12,buffer):
buffer: '-1234567890.12' (14 chars)
buffer: '-12345678901.2' (14 chars)
buffer: '-123456789012' (13 chars)
buffer: '-1.23456789012e+012' (19 chars)

buffer: '-12.3456789012' (14 chars)
buffer: '-1.23456789012' (14 chars)
buffer: '-0.123456789012' (15 chars)
buffer: '-1.23456789012e-002' (19 chars)

请参阅See also

数据转换Data Conversion
浮点支持Floating-Point Support
atof、_atof_l、_wtof、_wtof_latof, _atof_l, _wtof, _wtof_l
_ecvt_ecvt
_fcvt_fcvt