strtold, _strtold_l, wcstold, _wcstold_l

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

语法

long double strtold(
   const char *strSource,
   char **endptr
);
long double _strtold_l(
   const char *strSource,
   char **endptr,
   _locale_t locale
);
long double wcstold(
   const wchar_t *strSource,
   wchar_t **endptr
);
long double wcstold_l(
   const wchar_t *strSource,
   wchar_t **endptr,
   _locale_t locale
);

参数

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

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

locale
要使用的区域设置。

返回值

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

wcstold 返回类似于 strtold 的值。 对于这两个函数,如果发生溢出或下溢,则将 errno 设置为 ERANGE,并调用无效的参数处理程序,如参数验证中所述。

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

注解

每个函数均将输入字符串 strSource 转换为 long doublestrtold 函数在首个无法识别为数字一部分的字符处停止读取字符串 strSource。 这可能是终止空字符。 strtold 的宽字符版本是 wcstold;它的 strSource 参数是宽字符字符串。 否则,这些函数具有相同行为。

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

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcstold strtold strtold wcstold
_tcstold_l _strtold_l _strtold_l _wcstold_l

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

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

strtold 需要 strSource 指向以下形式的字符串:

[whitespace][sign][digits][.digits][{d | D | e | E}[sign]digits]

whitespace 可能包含被忽略的空格和制表符;sign 是加号 (+) 或减号 (-);digits 是一个或多个十进制数字。 如果基数字符前没有任何数字,则基数字符后必须至少有一个数字。 十进制数字可以后跟一个指数,其中包含介绍性字母(dDeE)和可选的带符号的整数。 如果指数部分和基数字符都没有出现,则假定基数字符跟随字符串中的最后一个数字。 不符合此形式的第一个字符将停止扫描。

要求

例程 必需的标头
strtold_strtold_l <stdlib.h>
wcstold_wcstold_l <stdlib.h> 或 <wchar.h>

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

示例

// crt_strtold.c
// Build with: cl /W4 /Tc crt_strtold.c
// This program uses strtold to convert a
// string to a long double-precision value.

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

int main( void )
{
   char *string;
   char *stopstring;
   long double x;

   string = "3.1415926535898This stopped it";
   x = strtold(string, &stopstring);
   printf("string = %s\n", string);
   printf("   strtold = %.13Lf\n", x);
   printf("   Stopped scan at: %s\n\n", stopstring);
}
string = 3.1415926535898This stopped it
   strtold = 3.1415926535898
   Stopped scan at: This stopped it

另请参阅

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