Example 1:
The only header file that we include here is the “iostream” to utilize the functions that are declared in this header file. Below this, we insert the “std” namespace. So, it makes it easy for us to utilize the function without placing this “std” with all functions each time when they are used in the code. Then, we invoke the “main()” function.
After this, we initialize the “my_name” and the variable’s data type is “string.” We assign “Samuel” to this variable and initialize the “my_age” variable. The data type of the “my_age” variable is “int” and we assign “24” to it. Now, we want to convert this “24” to the string. So, we make use of the “to_string()” function here. We initialize another variable named “ageToString” with the “string” data type.
Here, we utilize the “to_string()” function and assign it to this “ageToString” variable. We pass the “my_age” variable that contains the “int” data into this “to_string()” function as the parameter. This transforms the “my_age” integer value into the “string”. The converted data is then stored in the “ageToString” variable and displayed using “cout”.
Code 1:
using namespace std;
int main() {
string my_name = "Samuel";
int my_age = 24;
string ageToString = to_string(my_age);
cout<< "We are converting the integer into a string." << endl;
cout << my_name + " is " + ageToString + " years old";
}
Output:
The integer value is now converted into the string value and displayed here. This is because our code utilized the “to_string()” function.
Example 2:
The “iostream” is included first, and the “namespace std” is added here. Then, the “main()” function is called here. The “std_name” variable is initialized. Its data type is set to “string”. We assign “James” to this variable and then initialize the “marks” variable. This variable has the “float” data type and we give it the value of “90.5”.
Now, we want to utilize the “to_string()” function to transform this “90.5” into a string. We initialize the “string” data type variable called “marksToString” and place the “to_string()” function. We send the “marks” variable which holds the “float” data as an argument to the “to_string()” method.
Here, we assign the outcome of the “to_string()” function to the “marksToString” variable. This now changes the “marks” float value to the “string” data type and save it in the “marksToString” variable. The transformed data is then shown using “cout”.
Code 2:
using namespace std;
int main() {
string std_name = "James";
float marks = 90.5;
string marksToString = to_string(marks);
cout << std_name + " got " + marksToString + " marks";
}
Output:
The string value is converted from the float value and is shown here. This results from the “to_string()” method that is used in our code.
Example 3:
Now, we transform the “double” into the “string” data type. First, we initialize the “customer_name” of the “string” data type with the name “John”. Then, we place the variable of “double” data type where we assign the “9980.5” value to this variable. Now, we wish to convert this value which is a double data type into the “string” data type.
For this purpose, we utilize the “to_string” method here. We initialize the “salaryToString” variable of the “string” data type and place the “to_string()” method there. We pass the “salary” variable as its parameter. The salary is converted into the “string” data type and is stored in the “salaryToString” variable. Now, we print the “customer_name” along with the “salaryToString” by utilizing the “cout”. The data type of both variables is “string”.
Code 3:
using namespace std;
int main() {
string customer_name = "John";
double salary = 9980.5;
string salaryToString = to_string(salary);
cout << customer_name + " has " + salaryToString + " salary. ";
}
Output:
When we run this code, we can see the result in which the “double” data type value is now converted into the “string” data type and is displayed here along with the name.
Example 4:
Here, we convert both “int” and “float” data types into the “string”. For this, we initialize the “a” variable of the “int” data type with the integer value of “81” and the variable “b” of the “float” data type with the float value which is “72.9”.
In the following, we initialize two more variables with the names “firstStr” and “secondStr”, respectively, and assign the “to_string()” method here to both variables. Now, we pass “a” to the first “to_string()” function and “b” to the second “to_string()” method. Now, both values are converted into the “string” and stored in the “firstStr” and “secondStr”, respectively. After this, we print both string variables which we got after applying the “to_string()” method.
Code 4:
#include<string>
using namespace std;
int main()
{
int a = 81;
float b = 72.9;
string firstStr = to_string(a);
string secondStr = to_string(b);
cout<<"The string value of the integer a is: "<<firstStr << endl;
cout<<"The string value of the float b is: "<< secondStr << endl;
}
Output:
The integer value is first changed into the “string”. Then, the float is also changed into the “string”. Both string values are displayed here.
Example 5:
Here, we want to concatenate the integer or float values with the string but we don’t do this directly. Now, we have to transform the integer as well as the float values in the string. First, we apply concatenation and place the “to_string()” method in which we pass “9.8”. So, it changes this float number into the string and the concatenation is now done here.
After this, we apply this concatenation on the integer and string data by changing “int” into “string”. We place the integer values into the “to_string()” method. We have done both concatenations by changing them into the “string” data type and save then in the “s1” and “s2” variables, respectively. Then, we place “s1” and “s2” in the “cout” method. It also displays the result which we stored in these variables after changing them into strings and applying the concatenation of the strings data here.
Code 5:
#include <string>
using namespace std;
int main ()
{
string s1 = "The value of gravity is " + to_string(9.8);
string s2 = "The real number is " + to_string(4+8+9+10+14) + " here";
cout << s1 << endl ;
cout << s2 << endl ;
return 0;
}
Output:
First, the float value is transformed into a “string” and, subsequently, the integer value is transformed into a “string”. Both string values are shown here after concatenation with the other string data.
Example 6:
Now, we get the input from the user of the “int” data type and store it in the “x” variable as we declare the “x” variable of the “int” data type here. We get this input here with the aid of the “cin” command. Now, we convert this input into the “string” by placing the “x” variable into the “to_string()” method and then display the result that we stored in the “s” variable.
Code 6:
#include <string>
using namespace std;
int main ()
{
int x;
cout << "Please enter the number for converting it to string" <> x;
string s = to_string(x);
cout << "The converted integer to string is " + s << endl;
return 0;
}
Output:
After displaying the message here, we enter “84” which is the “int” data type and hit “enter”. Then, the converted result into the “string” data type is displayed below this.
Conclusion
The “to_string()” method is deeply studied in this guide. We explored the usage of this method in C++ programming. We learned that this method aids in converting “int”, “float”, as well as the “double” data types into the “string”. We demonstrated multiple examples in which we use this “to_string()” method in our codes and show the working of this method in this guide. We thoroughly studied this method here.