Home > Backend Development > C++ > Translate the following into Chinese: C++ program to convert string to float

Translate the following into Chinese: C++ program to convert string to float

WBOY
Release: 2023-09-13 11:57:02
forward
1276 people have browsed it

Translate the following into Chinese: C++ program to convert string to float

Static typing is used in C. In order to write a program, variables must be defined as specific types. Sometimes input from the console or a file must be read. In this case, the program is given string data. Special operations are required to convert them to other data types. This article will provide a C method to convert a string to a floating point integer. There are several different methods you can use to achieve this. Explore each of them separately.

Using string streams in C

Streams are a great tool in C. File streams, standard input/output streams, etc. are examples of these streams. stringstream is a different stream. It operates by accepting a string as input, similar to other streams. We have to import the sstream header file to use stringstream. Stream data can be obtained using the insertion operator (>>) or the extraction operator (

grammar

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

To use a stream to read input of a specific type, the syntax is as follows -

grammar

<data type> variable;
streamObject >> variable;
Copy after login

Algorithm

Let’s look at the algorithm to understand how it overall works.

  • Take string object x as input
  • Create a stringstream object, named ss, and pass x to the object
  • Create a floating point variable xFloat
  • Store floating point numbers to xFloat using the insertion operator in ss

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;
}
Copy after login

Output

The given number is: 3.14159
6.5 more than the given number is: 9.64159
Copy after login

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

Using sscanf() in C

A comparable approach (which also works in C) is to use the sscanf() function. This function accepts a character array as input and a format string, just like the standard scanf() function. Now it reads the requested value from the string and appends it to the variable pointed to by the variable's address. Please see the syntax of the sscanf() function.

grammar

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

Algorithm

Let’s look at the algorithm to understand how it overall works.

  • Take the string x as input in a C character array format
  • Use the sscanf() function with parameters x, format string and variable address
  • Variable values ​​will be stored directly from the sscanf() function.

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;
}
Copy after login

Output

The given number is: 6.8
2.5 more than the given number is: 9.3
Copy after login
Copy after login

The application runs exactly the same as before, but there are a few things we have to pay attention to. The sscanf() method does not support C-like string objects. It takes a C-like character array. To achieve this, we use the c_str() method to convert the provided string argument into a C-like character array.

Using stof() in C

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

grammar

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

algorithm

  • Take string object x as input
  • xFloat = stoi( x )
  • Return xFloat as a float variable from the 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;
}
Copy after login

Output

The given number is: 6.8
2.5 more than the given number is: 9.3
Copy after login
Copy after login

Using atof() in C

Although atof() is also available in C language, it is equivalent to stof. Strings can be submitted using character array format. You can get it by importing the cstdlib library. Otherwise, there's no real difference. Let's check the syntax.

grammar

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

algorithm

  • Take the string object x as input, in C language character array format.
  • xFloat = atoi( x )
  • Return xFloat as a floating point variable from the 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;
}
Copy after login

Output

The given number is: 8.9
6.5 more than the given number is: 15.4
Copy after login

in conclusion

There are multiple ways to convert a string to a floating point number. The first two methods (using stringstream and sscanf()) are general ways to convert a string to any data type without changing anything else; the only thing that changes is the type of the final variable. stof() and atof() These functions are only used to convert strings to floating point numbers. Other functions that convert to different data types are equivalent. Since they are C-based functions, sscanf and atof() do not accept string objects. Before using them, we have to convert the string to a character array using the c_str() function.

The above is the detailed content of Translate the following into Chinese: C++ program to convert string to float. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template