首页 > 文章列表 > 将以下内容翻译为中文:C++程序将字符串转换为浮点数

将以下内容翻译为中文:C++程序将字符串转换为浮点数

浮点数 转换 关键词:字符串
423 2023-08-19

Static typing is used in the C++. Variables must be defined with a specific type in order to write programmes. Inputs from the console or files must occasionally be read. In this case, the programme is given the string data. Special operations are necessary to transform them into other datatypes. This article will provide the C++ method for converting strings to floating point integers. A couple different methods can be used to accomplish this. Explore each of them separately.

Using stringstream in C++

流是C++中的一个很棒的工具。文件流、标准输入/输出流等都是这些流的例子。stringstream是一个不同的流存在。它通过接受一个字符串作为输入来进行操作,类似于其他流。我们必须导入sstream头文件才能使用stringstream。可以使用插入运算符(>>)或提取运算符(<<)来获取流数据。

语法

#include < sstream >
stringstream streamObject ( <a string input> );

To use the stream to read a specific type of input, the syntax will be like the below −

语法

<data type> variable;
streamObject >> variable;

Algorithm

Let us see the algorithm to understand how this works as a whole.

  • 将字符串对象x作为输入
  • <li>创建一个stringstream对象,命名为ss,并将x传递给该对象</li>
  • 创建一个浮点变量 xFloat
  • Use insertion operator from ss to store floating point into xFloat

Example

#include <iostream>
#include <sstream>

using namespace std;
float solve( string myString) {
   float x;
   stringstream ss( myString );
   ss >> x;
   return x;
}

int main()
{
   string aNumber = "3.14159";
   float convNumber = solve( aNumber );
   cout << "The given number is: " << convNumber << endl;
   cout << "6.5 more than the given number is: " << convNumber + 6.5 <<
   endl;
}

输出

The given number is: 3.14159
6.5 more than the given number is: 9.64159

It is obvious from this example that the number was retrieved from a string object. And because this is actual floating-point data, we can add 6.5 to itself in floating-point notation and display the result.

在C++中使用sscanf()

一种可比较的方法(在C中也适用)是使用sscanf()函数。该函数接受一个字符数组作为输入和格式字符串,就像标准的scanf()函数一样。现在它从字符串中读取所请求的值,并将其附加到由变量的地址指向的变量上。请查看sscanf()函数的语法。

语法

scanf ( <a string input>, <format string>, address(s) of variable );

Algorithm

Let us see the algorithm to understand how this works as a whole.

  • Take a string x as input in C-like character array format
  • Use the sscanf() function with parameter x, the format string, and the variable address
  • 变量值将直接从sscanf()函数中存储。

Example

#include <iostream>
#include <sstream>

using namespace std;
float solve( string myString) {
   float x;
   sscanf( myString.c_str(), "%f", &x );
   return x;
}

int main()
{
   string aNumber = "6.8";
   float convNumber = solve( aNumber );
   cout << "The given number is: " << convNumber << endl;
   cout << "2.5 more than the given number is: " << convNumber + 2.5 <<
   endl;
}

输出

The given number is: 6.8
2.5 more than the given number is: 9.3

The application is operating exactly as it did before, however there is something we must note. The C++-like string object is not supported by the sscanf() method. It requires a character array a la C. To achieve this, we turned the supplied string parameter into a character array akin to C using the c_str() method.

在C++中使用stof()

Using the stof() method from the "string" header file is another quick and simple way to convert a string to an integer. This function transforms a string object into its corresponding floating point number after receiving it as input.

语法

#include <string>
stof ( <integer in string format> );

Algorithm

  • 将字符串对象x作为输入
  • xFloat = stoi( x )
  • return xFloat as floating variable from given string x.

Example

#include <iostream>
#include <sstream>

using namespace std;
float solve( string myString) {
   float x;
   x = stof( myString );
   return x;
}

int main()
{
   string aNumber = "6.8";
   float convNumber = solve( aNumber );
   cout << "The given number is: " << convNumber << endl;
   cout << "2.5 more than the given number is: " << convNumber + 2.5 <<
   endl;
}

输出

The given number is: 6.8
2.5 more than the given number is: 9.3

在C++中使用atof()

Although the atof() is also available in C, it is pretty comparable to the stof. The string can be submitted using character array format. By importing the cstdlib library, you can get to it. Otherwise, there is no real distinction. Let's check out the syntax.

语法

#include <cstdlib>
atof ( <floating number in character array format> );

Algorithm

  • 将字符串对象x作为输入,以C语言字符数组格式。
  • xFloat = atoi( x )
  • return xFloat as floating point variable from given string x.

Example

#include <iostream>
#include <sstream>

using namespace std;
float solve( string myString) {
   float x;
   x = atof( myString.c_str() );
   return x;
}

int main()
{
   string aNumber = "8.9";
   float convNumber = solve( aNumber );
   cout << "The given number is: " << convNumber << endl;
   cout << "6.5 more than the given number is: " << convNumber + 6.5 <<
   endl;
}

输出

The given number is: 8.9
6.5 more than the given number is: 15.4

结论

There are several ways to convert strings to floating point numbers. The first two methods, using stringstream and sscanf(), are the generic ways to convert a string to any datatype without changing anything else; the only thing that will change is the type of the final variable. These functions, stof() and atof(), are solely used to convert strings to floats. Other functions that convert into different datatypes are equivalent. Since they are C-based functions, sscanf and atof() do not take string objects. Before using them, we must use the c_str() function to turn our string into a character array.