c语言atof函数_atof()函数与C ++中的示例

迈不过友情╰ 2023-03-05 12:44 43阅读 0赞

c语言atof函数

C ++ atof()函数 (C++ atof() function)

atof() function is a library function of cstdlib header. It is used to convert the given string value to the double value. It accepts a string containing a floating-point number and returns its double value.

atof()函数cstdlib标头的库函数。 它用于将给定的字符串值转换为双精度值。 它接受包含浮点数的字符串,并返回其双精度值。

Syntax of atof() function:

atof()函数的语法:

C++11:

C ++ 11:

  1. double atof (const char* str);

Parameter(s):

参数:

  • str – represents a string containing a floating-point number.

    str –表示包含浮点数的字符串。

Return value:

返回值:

The return type of this function is double, it returns the double converted value.

该函数的返回类型为double ,它返回double转换后的值。

Example:

例:

  1. Input:
  2. str = "123.456";
  3. Function call:
  4. atof(str);
  5. Output:
  6. 123.456

C ++代码演示atof()函数的示例 (C++ code to demonstrate the example of atof() function)

  1. // C++ code to demonstrate the example of
  2. // atof() function
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <string.h>
  6. using namespace std;
  7. // main() section
  8. int main()
  9. {
  10. char str[50];
  11. strcpy(str, "123.456");
  12. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  13. strcpy(str, "-123.456");
  14. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  15. strcpy(str, "0.123456");
  16. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  17. strcpy(str, "-0.123456");
  18. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  19. strcpy(str, "12345678.123456");
  20. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  21. strcpy(str, "-12345678.123456");
  22. cout << "atof(\"" << str << "\"): " << atof(str) << endl;
  23. return 0;
  24. }

Output

输出量

  1. atof("123.456"): 123.456
  2. atof("-123.456"): -123.456
  3. atof("0.123456"): 0.123456
  4. atof("-0.123456"): -0.123456
  5. atof("12345678.123456"): 1.23457e+07
  6. atof("-12345678.123456"): -1.23457e+07

Reference: C++ atof() function

参考: C ++ atof()函数

翻译自: https://www.includehelp.com/cpp-tutorial/atof-function-with-example.aspx

c语言atof函数

发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读