将浮点数转换为字符串。Converts a floating-point number to a string. 这是 _fcvt 版本,具有 CRT 中的安全功能中所述的安全增强功能。This is a version of _fcvt with security enhancements as described in Security Features in the CRT.


errno_t _fcvt_s(   
   char* buffer,  
   size_t sizeInBytes,  
   double value,  
   int count,  
   int *dec,  
   int *sign   
template <size_t size>  
errno_t _fcvt_s(   
   char (&buffer)[size],  
   double value,  
   int count,  
   int *dec,  
   int *sign   
); // C++ only  


[out] buffer[out] buffer
所提供的缓冲区将保留转换的结果。The supplied buffer that will hold the result of the conversion.

[in] sizeInBytes[in] sizeInBytes
缓冲区的大小(以字节为单位)。The size of the buffer in bytes.

[in] value[in] value
要转换的数字。Number to be converted.

[in] count[in] count
小数点后面的数字位数。Number of digits after the decimal point.

[out] dec[out] dec
指向存储的小数点位置的指针。Pointer to the stored decimal-point position.

[out] sign[out] sign
指向存储的符号指示符的指针。Pointer to the stored sign indicator.

返回值Return Value

如果成功,则返回 0。Zero if successful. 如果失败,则返回值为错误代码。The return value is an error code if there is a failure. 错误代码是在 ERRNO.h 中定义的。Error codes are defined in Errno.h. 有关这些错误的列表,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor a listing of these errors, see errno, _doserrno, _sys_errlist, and _sys_nerr.

对于无效参数(如下表中所列),此函数调用无效参数处理程序,如参数验证中所述。In the case of an invalid parameter, as listed in the following table, this function invokes the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则该函数将 errno 设置为 EINVAL 并返回 EINVALIf execution is allowed to continue, this function sets errno to EINVAL and returns EINVAL.

错误条件Error Conditions

buffer sizeInBytes value countcount decdec Signsign 返回Return buffer 中的值Value in buffer
NULL 任何any 任何any 任何any 任何any 任何any EINVAL 未修改。Not modified.
NULL(指向有效内存)Not NULL (points to valid memory) <=0<=0 任何any 任何any 任何any 任何any EINVAL 未修改。Not modified.
任何any 任何any 任何any 任何any NULL 任何any EINVAL 未修改。Not modified.
任何any 任何any 任何any 任何any 任何any NULL EINVAL 未修改。Not modified.

安全问题Security Issues

如果 buffer 不指向有效内存且不为 NULL,则 _fcvt_s 可能生成访问冲突。_fcvt_s might generate an access violation if buffer does not point to valid memory and is not NULL.


_fcvt_s 函数将浮点数转换为以 null 结尾的字符串。The _fcvt_s function converts a floating-point number to a null-terminated character string. value 参数是要转换的浮点数。The value parameter is the floating-point number to be converted. _fcvt_svalue 的位数存储为字符串,并追加一个空字符 ('\0')。_fcvt_s stores the digits of value as a string and appends a null character ('\0'). count 参数指定此小数点后要存储的数字位数。The count parameter specifies the number of digits to be stored after the decimal point. 多余的位数被舍入到 count 位置。Excess digits are rounded off to count places. 如果小于精确到 count 的位数,则字符串使用零填充。If there are fewer than count digits of precision, the string is padded with zeros.

字符串中仅存储位数。Only digits are stored in the string. 小数点位置和 value 的符号可以在调用后从 decsign 中获取。The position of the decimal point and the sign of value can be obtained from dec and sign after the call. dec 参数指向整数值;此整数值指定相对于字符串开头的小数点的位置。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. 参数 sign 指向一个整数值,表示 value 的符号。The parameter sign points to an integer indicating the sign of value. 如果 value 为正数,则整数设置为 0,如果 value 为负数,则整数设置为非零数。The integer is set to 0 if value is positive and is set to a nonzero number if value is negative.

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

_ecvt_s_fcvt_s 之间的差异在于对 count 参数的解释。The difference between _ecvt_s and _fcvt_s is in the interpretation of the count parameter. _ecvt_s 解释count作为的输出字符串中的位数总数和_fcvt_s解释count作为小数点后的数字个数。_ecvt_s interprets count as the total number of digits in the output string, and _fcvt_s interprets count as the number of digits after the decimal point.

在 C++ 中,通过模板重载简化此函数的使用;重载可以自动推导出缓冲区长度,不再需要指定大小参数。In C++, using this function is simplified by a template overload; the overload can infer buffer length automatically, eliminating the need to specify a size argument. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

此函数的调试版本首先使用 0xFD 填充缓冲区。The debug version of this function first fills the buffer with 0xFD. 若要禁用此行为,请使用 _CrtSetDebugFillThresholdTo disable this behavior, use _CrtSetDebugFillThreshold.


函数Function 必需的标头Required header 可选标头Optional header
_fcvt_s <stdlib.h><stdlib.h> <errno.h><errno.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.

库: CRT 库功能的所有版本。Libraries: All versions of the CRT Library Features.


// fcvt_s.c  
#include <stdio.h>  
#include <stdlib.h>  
#include <errno.h>  

int main()  
  char * buf = 0;  
  int decimal;  
  int sign;  
  int err;  

  buf = (char*) malloc(_CVTBUFSIZE);  
  err = _fcvt_s(buf, _CVTBUFSIZE, 1.2, 5, &decimal, &sign);  

  if (err != 0)  
     printf("_fcvt_s failed with error code %d\n", err);  

  printf("Converted value: %s\n", buf);    

Converted value: 120000  

请参阅See Also

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