strtod ()-strtof ()-str大会堂 ()- 将字符串转换为 Double , Float 和 Long Double

格式

#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);

语言级别

ANSI

线程安全

语言环境敏感

这些函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_NUMERIC 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

strtod()strtof()strtold() 函数将字符串转换为双精度值,浮点值或长整型双精度值。 参数 nptr 指向可以解释为数字二进制浮点值的字符序列。 这些函数在未识别为数字一部分的第一个字符处停止读取字符串。 此字符可以是字符串末尾的空字符。

strtod()strtof()strtold() 函数期望 nptr 指向具有以下格式的字符串:

读取语法图跳过可视语法图空格 +  – 数字.数字.数字eE +  – 数字0xX六位数字.六位数字.六位数字pP +  – 数字

不适合此表单的第一个字符将停止扫描。 此外,INFINITYNAN(忽略大小写) 是允许的。

如果使用十六进制数字格式指定了指数,那么该指数将解释为二进制 (基本 2) 指数。 如果使用十进制数字格式指定指数,那么该指数将解释为十进制 (以 10 为底) 指数。

返回值

strtod()strtof()strtold() 函数返回浮点数的值,除非表示导致下溢或溢出。 对于溢出, strtof() 返回 HUGE_VALF-HUGE_VALF; strtod()strtold() 返回 HUGE_VAL-HUGE_VAL。 对于下流,所有函数都返回 0

在这两种情况下, errno 都设置为 ERANGE。 如果 nptr 所指向的字符串没有期望的格式,那么不会执行任何转换,并且 nptr 的值存储在 endptr所指向的对象中,前提是 endptr 不是 NULL 指针。

如果数字以外的字符跟在作为指数读取的 Ee 之后,那么 strtod()strtof()strtold() 函数不会失败。 例如, 100elf 将转换为浮点值 100.0

字符序列INFINITY(忽略大小写) 生成值 INFINITY。 字符值NAN生成 "安静非数字" (NAN) 值。

示例

此示例将字符串转换为双精度值,浮点值和长整型双精度值。 它将打印已转换的值以及停止转换的子串。
#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   char *string, *stopstring;
   double x;
   float f;
   long double ld;

   string = "3.1415926This stopped it";
   f = strtof(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtof = %f\n", f);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);
   string = "100ergs";
   f = strtof(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtof = %f\n", f);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);

   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 = "100ergs";
   x = strtod(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtod = %f\n", x);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);

   string = "3.1415926This stopped it";
   ld = strtold(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtold = %lf\n", ld);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);
   string = "100ergs";
   ld = strtold(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtold = %lf\n", ld);
   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"

*/

相关信息