Home > Article > Backend Development > 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.
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 (
#include < sstream > stringstream streamObject ( <a string input> );
To use a stream to read input of a specific type, the syntax is as follows -
<data type> variable; streamObject >> variable;
Let’s look at the algorithm to understand how it overall works.
#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 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.
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.
scanf ( <a string input>, <format string>, address(s) of variable );
Let’s look at the algorithm to understand how it overall works.
#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 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 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.
#include <string> stof ( <integer in string format> );
#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
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.
#include <cstdlib> atof ( <floating number in character array format> );
#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 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!