将字符串转换为二进制文件。
double atof(
const char *str
);
double _atof_l(
const char *str,
_locale_t locale
);
double _wtof(
const wchar_t *str
);
double _wtof_l(
const wchar_t *str,
_locale_t locale
);
参数
str
将转换的字符串。locale
使用的区域设置。
返回值
每个函数返回解释生成的 double 值类型字符为数字。,如果输入无法转换为该类型,的值返回值为 0.0。
在所有超出范围的情况下, errno 设置为 ERANGE。如果传递的参数是 NULL,无效参数调用处理程序,如中所 参数验证述。如果执行允许继续,对 EINVAL 的这些功能集 errno 并且返回 0。
备注
这些函数将字符串转换为双精度,浮点值。
输入字符串是可被解释为指定类型的一个数值字符的序列。函数停止读取输入字符串在作为数字的一部分,它无法识别的第一个字符。此字符可以是) 终止字符串的在 null 字符 (“\ 0 " 或 L' \ 0 "。
为 atof 和 _wtof 的 str 参数具有以下形式:
[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
whitespace 包括空格或制表符,将忽略; sign 加号 (+) 或减号 (-);并 digits 是一个或多个十进制数字。如果数字未出现,请在小数点前,至少一个必须在后面小数点。十进制数字能由指数按照,包括一个表示字母 (d、 D、 e或 E) 和一个可选择签名的十进制整数。
这些功能的版本与 _l 后缀的相同,只不过它们使用区域设置参数而不是当前区域设置。
一般文本例程映射
TCHAR.H 实例 |
未定义的 _UNICODE & _MBCS |
定义的 _MBCS |
定义的 _UNICODE |
---|---|---|---|
_tstof |
atof |
atof |
_wtof |
_ttof |
atof |
atof |
_wtof |
要求
实例 |
必需的头 |
---|---|
atof |
<math.h> 和 <stdlib.h> |
_atof_l |
<math.h> 和 <stdlib.h> |
_wtof, _wtof_l |
<stdlib.h> 或 <wchar.h> |
示例
本过程演示作为字符串存储的数字中使用 atof 功能,如何转换为数值。
// crt_atof.c
//
// This program shows how numbers stored as
// strings can be converted to numeric
// values using the atof function.
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char *str = NULL;
double value = 0;
// An example of the atof function
// using leading and training spaces.
str = " 3336402735171707160320 ";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
// Another example of the atof function
// using the 'd' exponential formatting keyword.
str = "3.1412764583d210";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
// An example of the atof function
// using the 'e' exponential formatting keyword.
str = " -2309.12E-15";
value = atof( str );
printf( "Function: atof( \"%s\" ) = %e\n", str, value );
}