zoukankan      html  css  js  c++  java
  • c语言strtod()函数的用法

    函数原型:

    #include <stdlib.h>

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

    C语言及C++中的重要函数。

      名称含义
      strtod(将字符串转换成浮点数

      相关函数

      atoi,atol,strtod,strtol,strtoul
      
      函数说明

      strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('')才结束转换,并将结果返回。
        若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。

      返回值

      返回转换后的浮点型数。
     

      附加说明

      参考atof()。

    范例
    #include<stdlib.h>
    #include<stdio.h>
    void main()
    {
    char *endptr;
    char a[] = "12345.6789";
    char b[] = "1234.567qwer";
    char c[] = "-232.23e4";
    printf( "a=%lf ", strtod(a,NULL) );
    printf( "b=%lf ", strtod(b,&endptr) );
    printf( "endptr=%s ", endptr );
    printf( "c=%lf ", strtod(c,NULL) );
    }
    执行结果:
    a=12345.678900
    b=1234.567000
    endptr=qwer
    c=-2322300.000000

    补充说明:
    附类同的atof函数,atof函数是需要确定a是数字类型的字符串;
    -------
    atof
    1. 函数名: atof
    功 能: 把字符串转换成浮点数
    名字来源:ascii to floating point numbers 的缩写
    用 法: double atof(const char *nptr);
    
    
    中文名
    atof()
    外文名
    ascii to floating point numbers
    释    义
    . 函数名
    功 能
     把字符串转换成浮点数
     
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
    double d;
    char str[] = "123.456";
    d=atof(str);
    printf("string=%sdouble=%lf ",str,d);
    return 0;
    }
    
    
     
    基本介绍
    2. atof(将字串转换成浮点型数)
    
    
    相关函数 atoiatolstrtodstrtolstrtoul
    
    
    表头文件 #include <stdlib.h>
    
    
    定义函数 double atof(const char *nptr);
    
    
    函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
    
    
    返回值 返回转换后的浮点型数。
    
    
    附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。
    
    
    范例 /* 将字符串a 与字符串b转换成数字后相加*/
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include<stdlib.h>
    int main()
    {
    char*a="-100.23";
    char*b="200e-2";
    doublec;
    c=atof(a)+atof(b);
    printf(“c=%.2lf ”,c);
    return 0;
    }
    
    
    执行 c=-98.23
     
  • 相关阅读:
    gtest(C++单元测试框架)
    tinyXML入门
    笔记 解决vue3动态绑定本地图片失效问题
    面试技巧
    vuex 状态管理
    插槽的使用
    Vue-router 路由
    Vue组件
    (转)JS 常用 DOM
    9-26
  • 原文地址:https://www.cnblogs.com/the-tops/p/5889974.html
Copyright © 2011-2022 走看看