Converting Number to String in C++

11 Jun, 2022
Converting numbers to string or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. But lack of knowledge of certain essential tools binds us to do so. Some methods to achieve this task are mentioned in this article.
There are 3 major methods to convert a number to a string, which are as follows:
Using string Stream 
Using to_string()
Using boost lexical cast
In this method, a string stream declares a stream object which first inserts a number, as a stream into an object and then uses “str()” to follow the internal conversion of a number to a string. 
Example:   
CPP
// C++ code to demonstrate string stream method// to convert number to string.#include<iostream>#include <sstream>  // for string streams#include <string>  // for stringusing namespace std;int main(){    int num = 2016;     // declaring output string stream    ostringstream str1;     // Sending a number as a stream into output    // string    str1 << num;     // the str() converts number into string    string geek = str1.str();     // Displaying the string    cout << "The newly formed string from number is : ";    cout << geek << endl;     return 0;}
The newly formed string from number is : 2016
Time Complexity: O(n)
Auxiliary Space: O(n)
 The function to_string() accepts a number(which can be any data type) and returns the number in the desired string. 
CPP
// C++ code to demonstrate "to_string()" method// to convert number to string.#include <iostream>#include <string> // for string and to_string()using namespace std; // Driver Codeint main(){    // Declaring integer    int i_val = 20;     // Declaring float    float f_val = 30.50;     // Conversion of int into string using    // to_string()    string stri = to_string(i_val);     // Conversion of float into string using    // to_string()    string strf = to_string(f_val);     // Displaying the converted strings    cout << "The integer in string is : ";    cout << stri << endl;    cout << "The float in string is : ";    cout << strf << endl;     return 0;}
The integer in string is : 20
The float in string is : 30.500000
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar to string conversion, the ” lexical_cast() ” function remains the same, but in ‘boost lexical cast‘ time argument list modifies to “lexical_cast(numeric_var).  
Example:
CPP
// C++ code to demonstrate "lexical_cast()" method// to convert number to string.#include <boost/lexical_cast.hpp> // for lexical_cast()#include <iostream>#include <string> // for stringusing namespace std; // Driver Codeint main(){    // Declaring float    float f_val = 10.5;     // Declaring int    int i_val = 17;     // lexical_cast() converts a float into string    string strf = boost::lexical_cast<string>(f_val);     // lexical_cast() converts a int into string    string stri = boost::lexical_cast<string>(i_val);     // Displaying string converted numbers    cout << "The float value in string is : ";    cout << strf << endl;    cout << "The int value in string is : ";    cout << stri << endl;     return 0;}
The float value in string is : 10.5
The int value in string is : 17
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 
SaloniBansal1
simmytarika5
23620uday2021
anshikajain26
harsh_shokeen
surbhikumaridav
CPP-Library
cpp-string
cpp-stringstream
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
std::string class in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++