将浮点数转换为字符串。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


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

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

要转换的数字。Number to be converted.

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

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

指向存储的符号指示符的指针。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. 如果允许执行继续,此函数将errnoEINVAL并返回EINVALIf execution is allowed to continue, this function sets errno to EINVAL and returns EINVAL.

错误条件Error Conditions

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

安全性问题Security Issues

_fcvt_s可能会产生访问冲突,如果缓冲区不指向有效内存并且不是NULL_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. 参数是要转换的浮点数。The value parameter is the floating-point number to be converted. _fcvt_s存储的位数作为字符串,并追加 null 字符 (\0)。_fcvt_s 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. 小数点和的符号的位置可以从获取dec登录后调用。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. 参数登录指向一个整数,表示的符号The parameter sign points to an integer indicating the sign of value. 整数设置为 0,如果为正数,并且设置为非零的数字如果为负。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处于的解释计数参数。The difference between _ecvt_s and _fcvt_s is in the interpretation of the count parameter. _ecvt_s解释计数作为的输出字符串中的位数总数和 _fcvt_s解释计数后的位数的数字的形式小数点。_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_fcvt_s <stdlib.h><stdlib.h> <errno.h><errno.h>

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

库: 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_latof, _atof_l, _wtof, _wtof_l