手机版
你好,游客 登录 注册
背景:
阅读新闻

C语言中如何将字符串转换成float和double类型

[日期:2013-12-07] 来源:Linux社区  作者:hzhsan [字体:大 中 小]

C语言中如何将字符串转换成float和double类型

先贴上可编译运行的源代码:

file: a.cpp

#include <stdio.h>

#include <stdlib.h>
 int main ()
 {
        char szOrbits[] ="365.24";
        char* pEnd;
        float f1;
        f1 = strtof (szOrbits, &pEnd);
        printf("%f\n",f1);
        return 0;
 }

执行结果:

[tuxedo@imorcl yali_test]$ g++ a.cpp -o aaa
[tuxedo@imorcl yali_test]$ ./aaa
365.239990

 

man参考手册:在linux上 man strtod就能显示

NAME
      strtod, strtof, strtold - convert ASCII string to floating point number

SYNOPSIS
      #include <stdlib.h>

      double strtod(const char *nptr, char **endptr);

      #define _XOPEN_SOURCE=600  /* or #define _ISOC99_SOURCE */
      #include <stdlib.h>

      float strtof(const char *nptr, char **endptr);
      long double strtold(const char *nptr, char **endptr);

DESCRIPTION
      The  strtod(),  strtof(),  and  strtold() functions convert the initial portion of the string pointed to by nptr to double, float, and
      long double representation, respectively.

      。。。

推荐阅读:

C++ 隐式类类型转化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C语言变长数组之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C语言需要注意的问题 http://www.linuxidc.com/Linux/2013-05/84301.htm

C语言位域的使用及其注意点 http://www.linuxidc.com/Linux/2013-07/87027.htm

C语言中简单的for循环和浮点型变量 http://www.linuxidc.com/Linux/2013-08/88514.htm

《C语言从入门到精通》.(王娣,韩旭 ).[PDF] + DVD视频光盘文件 http://www.linuxidc.com/Linux/2013-10/91775.htm

linux
相关资讯       C语言字符串转换  C语言float和double 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款