Converting Number to String in C++

In C++, converting integers to strings or converting numbers to strings 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. Let’s look at some methods to convert an integer or a number to a string.

Converting Number to String in C++

There are 4 major methods to convert a number to a string, which are as follows:

• Using to_string()
• Using string Stream
• Using sprintf() function
• Using boost lexical cast

Method 1: Using to_string()

The to_string() function can be used to convert an integer, floating point values, or any number to a string. This function accepts a number(which can be any data type) and returns the number as the desired string.

Syntax:

`string to_string (int val);`

Parameters:

• val – Any Numerical Value.

Return Value:

• A string object containing the representation of value as a sequence of characters.

Example:

C++

 `// C++ code to demonstrate "to_string()" method` `// to convert number to string.` `#include ` `#include // for string and to_string()` `using` `namespace` `std;`   `// Driver Code` `int` `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;` `}`

Output

```The integer in string is : 20
The float in string is : 30.500000```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 2: Using string streams

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:

C++

 `// C++ code to demonstrate string stream method` `// to convert number to string.` `#include` `#include   // for string streams` `#include   // for string` `using` `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;` `}`

Output

`The newly formed string from number is : 2016`

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using the sprintf() function

sprintf() function stores the output on the char buffer specified in the function, instead of printing the output on the console.

C++

 `// C++ Program to illustrate the use of sprintf() for number` `// to string conversion` `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``// any num` `    ``int` `n = 12234;` `    ``// string buffer` `    ``char` `str[1000];`   `    ``// sprintf() to print num to str buffer` `    ``sprintf``(str, ``"%d"``, n);`   `    ``cout << ``"the string is : "` `<< str;`   `    ``return` `0;` `}`   `// this code is contributed by shivanisingh`

Output

`the string is : 12234`

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using boost lexical cast

Similar to string conversion, the ” lexical_cast() ” function remains the same, but in the ‘boost lexical cast‘ time argument list modifies to “lexical_cast(numeric_var).

Example:

C++

 `// C++ code to demonstrate "lexical_cast()" method` `// to convert number to string.` `#include // for lexical_cast()` `#include ` `#include // for string` `using` `namespace` `std;`   `// Driver Code` `int` `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(f_val);`   `    ``// lexical_cast() converts a int into string` `    ``string stri = boost::lexical_cast(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;` `}`

Output

```The float value in string is : 10.5
The int value in string is : 17```

Time Complexity: O(n)
Auxiliary Space: O(n)

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next