strtod32()- strtod64()- strtod128()- 将字符串转换为十进制浮点数
格式
#define __STDC_WANT_DEC_FP__
#include <stdlib.h>
_Decimal32 strtod32(const char *nptr, char **endptr);
_Decimal64 strtod64(const char *nptr, char **endptr);
_Decimal128 strtod132(const char *nptr, char **endptr);语言级别
ANSI
线程安全
是
语言环境敏感
这些函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_NUMERIC 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境。
描述
strtod32(), strtod64()和 strtod128() 函数将字符串转换为单精度,双精度或四精度十进制浮点值。 参数 nptr 指向可解释为数字十进制浮点值的字符序列。 这些函数在未识别为数字一部分的第一个字符处停止读取字符串。 此字符可以是字符串末尾的空字符。 endptr 参数将更新为指向此字符,前提是 endptr 不是 NULL 指针。
strtod32(), strtod64()和 strtod128() 函数期望 nptr 指向具有以下格式的字符串:
不适合此表单的第一个字符将停止扫描。 此外,INFINITY或NAN(忽略大小写) 是允许的。
返回值
strtod32(), strtod64()和 strtod128() 函数返回浮点数的值,除非表示导致下溢或溢出。 对于溢出, strtod32() 返回 HUGE_VAL_D32 或 -HUGE_VAL_D32; strtod64() 返回 HUGE_VAL_D64 或 -HUGE_VAL_D64; strtod128() 返回 HUGE_VAL_D128 或 -HUGE_VAL_D128。 对于下流,所有函数都返回 +0.E0。
在溢出和下流情况下, errno 都设置为 ERANGE。 如果 nptr 指向的字符串没有期望的格式,那么值为 +0.E0 ,并且 nptr 的值存储在 endptr指向的对象中,前提是 endptr 不是 NULL 指针。
如果数字以外的字符跟在作为指数读取的 E 或 e 之后,那么 strtod32(), strtod64()和 strtod128() 函数不会失败。 例如, 100elf 将转换为浮点值 100.0。
字符序列INFINITY(忽略大小写) 生成值 INFINITY。 字符值NAN生成 "安静非数字" (NaN) 值。
如果需要,将使用舍入方式舍入到 "最近" , "平均" 来对返回值进行舍入。
示例
#define __STDC_WANT_DEC_FP__
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string, *stopstring;
_Decimal32 d32;
_Decimal64 d64;
_Decimal128 d128;
string = "3.1415926This stopped it";
d32 = strtod32(string, &stopstring);
printf("string = %s\n", string);
printf(" strtod32 = %Hf\n", d32);
printf(" Stopped scan at %s\n\n", stopstring);
string = "100ergs";
d32 = strtod32(string, &stopstring);
printf("string = \"%s\"\n", string);
printf(" strtof = %Hf\n", d32);
printf(" Stopped scan at \"%s\"\n\n", stopstring);
string = "3.1415926This stopped it";
d64 = strtod64(string, &stopstring);
printf("string = %s\n", string);
printf(" strtod = %Df\n", d64);
printf(" Stopped scan at %s\n\n", stopstring);
string = "100ergs";
d64 = strtod64(string, &stopstring);
printf("string = \"%s\"\n", string);
printf(" strtod = %Df\n", d64);
printf(" Stopped scan at \"%s\"\n\n", stopstring);
string = "3.1415926This stopped it";
d128 = strtod128(string, &stopstring);
printf("string = %s\n", string);
printf(" strtold = %DDf\n", d128);
printf(" Stopped scan at %s\n\n", stopstring);
string = "100ergs";
d128 = strtod128(string, &stopstring);
printf("string = \"%s\"\n", string);
printf(" strtold = %DDf\n", d128);
printf(" Stopped scan at \"%s\"\n\n", stopstring);
}
/***************** Output should be similar to: *****************
string = 3.1415926This stopped it
strtof = 3.141593
Stopped scan at This stopped it
string = "100ergs"
strtof = 100.000000
Stopped scan at "ergs"
string = 3.1415926This stopped it
strtod= 3.141593
Stopped scan at This stopped it
string = "100ergs"
strtod = 100.000000
Stopped scan at "ergs"
string = 3.1415926This stopped it
strtold = 3.141593
Stopped scan at This stopped it
string = "100ergs"
strtold = 100.000000
Stopped scan at "ergs"
*/
相关信息
- atof ()-将字符串转换为浮点
- atoi ()-将字符串转换为整数
- atol ()-atoll ()-将字符串转换为长整型或长整型整数
- strtod ()-strtof ()-str被告 ()-将字符串转换为 Double , Float 和 Long Double
- strtol ()-strtol()-将字符串转换为长整型和长整型整数
- strtoul ()-strtoull ()-将字符串转换为无符号长整型和无符号长整型整数
- wcstod ()-wcstof ()-wcstold ()-将宽字符字符串转换为 Double , Float 和 Long Double
- wcstod32()- wcstod64()- wcstod128()-将宽字符字符串转换为十进制浮点
- <stdlib.h>
