strtod, _strtod_l, wcstod, _wcstod_l

将字符串转换为双精度值。

语法

double strtod(
   const char *strSource,
   char **endptr
);
double _strtod_l(
   const char *strSource,
   char **endptr,
   _locale_t locale
);
double wcstod(
   const wchar_t *strSource,
   wchar_t **endptr
);
double _wcstod_l(
   const wchar_t *strSource,
   wchar_t **endptr,
   _locale_t locale
);

参数

strSource
要转换的 null 终止的字符串。

endptr
指向停止扫描的字符的指针。

locale
要使用的区域设置。

返回值

strtod 返回浮点数值,只有当表示形式会导致溢出时,该函数才返回 +/-HUGE_VALHUGE_VAL 的符号与无法表示的值的符号相匹配。 如果无法执行转换或出现下溢,则 strtod 返回 0

wcstod 返回类似于 strtod 的值:

  • 对于这两个函数,如果出现溢出或下溢,则 errno 设置为 ERANGE
  • 如果有无效参数,则 errno 设置为 EINVAL 并调用无效参数处理程序,如参数验证中所述。

有关此及其他的返回代码的详细信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

备注

每个函数均将输入字符串 strSource 转换为 doublestrtod 函数将 strSource 转换为双精度值。 strtod 在首个无法识别为数字一部分的字符处停止读取字符串 strSource。 此字符可能是终止 null 字符。 wcstodstrtod 的宽字符版本;它的 strSource 参数是宽字符字符串。 否则这些函数具有相同行为。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcstod strtod strtod wcstod
_tcstod_l _strtod_l _strtod_l _wcstod_l

当前区域设置的 LC_NUMERIC 类别设置确定 strSource 中的基数点字符的识别。 有关详细信息,请参阅 setlocale。 不带 _l 后缀的函数使用当前区域设置;_strtod_l_strtod 相同,只不过前者改用传递的 locale。 有关详细信息,请参阅 Locale

如果 endptr 不为 NULL,则在 endptr 所指向的位置存储指向停止扫描的字符的指针。 如果无法执行任何转换(未找到任何有效的数字或指定了无效的基数),则将 strSource 的值存储在由 endptr 指向的位置。

strtod 需要 strSource 指向以下形式之一的字符串:

[whitespace] [sign] {digits [radixdigits] | radixdigits} [{e | E} [sign] digits]
[whitespace] [sign] {0x | 0X} {hexdigits [radixhexdigits] | radixhexdigits} [{p | P} [sign] digits]
[whitespace] [sign] {INF | INFINITY}
[whitespace] [sign] NAN [sequence]

可选前导 whitespace 可能包含被忽略的空格和制表符。
sign 是加 (+) 或减 (+)。
digits 是一个或多个十进制数字。
hexdigits 是一个或多个十六进制数字。
radix 是基数点字符,可以是默认“C”区域设置中的句点 (.),或者如果当前区域设置不同或指定 locale 时,则为区域设置特定的值。
sequence 是字母数字或下划线字符的序列。

在十进制和十六进制数字形式中,如果没有数字出现在基数点字符之前,则至少一个数字必须出现在弧度点字符之后。

在十进制形式中,十进制数字可以后跟一个指数,其中包含介绍性字母(eE)和可选的带符号的整数。

在十六进制形式中,十六进制数字可以后跟一个指数,其中包含介绍性字母(pP)和可选的带符号的十进制整数,该整数表示指数为 2 的幂。

在任一种形式中,如果没有指数部分或基数点字符,则假定基数点字符跟随字符串中的最后一个数字。

INFNAN 形式中都忽略大小写。 不符合这些形式之一的第一个字符将停止扫描。

这些函数的 UCRT 版本不支持转换 Fortran 样式的(dD)指数字母。 这个非标准扩展受早期版本的 CRT 支持,可能会为你的代码的带来重大变化。 UCRT 版本支持十六进制字符串和 INFNAN 值的往返,这在更早版本中不受支持。 此支持也会导致代码的中断性变更。 例如,字符串“0x1a”在早期版本中会被 strtod 解释为 0.0,但在 UCRT 版本中解释为 26.0。

要求

例程 必需的标头
strtod_strtod_l C:<stdlib.h> C++:<cstdlib><stdlib.h>
wcstod_wcstod_l C:<stdlib.h><wchar.h> C++:<cstdlib><stdlib.h><wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strtod.c
// This program uses strtod to convert a
// string to a double-precision value; strtol to
// convert a string to long integer values; and strtoul
// to convert a string to unsigned long-integer values.
//

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *string, *stopstring;
    double x;
    long   l;
    int    base;
    unsigned long ul;

    string = "3.1415926This stopped it";
    x = strtod(string, &stopstring);
    printf("string = %s\n", string);
    printf("   strtod = %f\n", x);
    printf("   Stopped scan at: %s\n\n", stopstring);

    string = "-10110134932This stopped it";
    l = strtol(string, &stopstring, 10);
    printf("string = %s\n", string);
    printf("   strtol = %ld\n", l);
    printf("   Stopped scan at: %s\n\n", stopstring);

    string = "10110134932";
    printf("string = %s\n", string);

    // Convert string using base 2, 4, and 8:
    for (base = 2; base <= 8; base *= 2)
    {
        // Convert the string:
        ul = strtoul(string, &stopstring, base);
        printf("   strtol = %ld (base %d)\n", ul, base);
        printf("   Stopped scan at: %s\n", stopstring);
    }

    // NaN
    x = strtod("+nan", &stopstring);
    printf("\n%f\n", x);

    // INF
    x = strtod("-INF", &stopstring);
    printf("\n%f\n", x);

    // e - exponent
    x = strtod("1.18973e+49", &stopstring);
    printf("\n%f\n", x);

    // doesn't handle Fortran style
    x = strtod("1.18973d+49", &stopstring);
    printf("\n%f\n", x);
    printf("No Fortran style support. Stopped parsing at %s\n", stopstring);
}
string = 3.1415926This stopped it
   strtod = 3.141593
   Stopped scan at: This stopped it

string = -10110134932This stopped it
   strtol = -2147483648
   Stopped scan at: This stopped it

string = 10110134932
   strtol = 45 (base 2)
   Stopped scan at: 34932
   strtol = 4423 (base 4)
   Stopped scan at: 4932
   strtol = 2134108 (base 8)
   Stopped scan at: 932

nan

-inf

11897299999999999421285862642874618947301378359296.000000

1.189730
No Fortran style support. Stopped parsing at d+49

另请参阅

数据转换
数学和浮点支持
多字节字符序列的解释
区域设置
字符串到数值函数
strtolwcstol_strtol_l_wcstol_l
strtoul_strtoul_lwcstoul_wcstoul_l
atof_atof_l_wtof_wtof_l
localeconv
_create_locale_wcreate_locale
_free_locale