VOOZH about

URL: https://www.geeksforgeeks.org/cpp/converting-number-to-string-in-cpp/

⇱ Converting Number to String in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Converting Number to String in C++

Last Updated : 27 May, 2026

In C++, converting numbers to strings is a very common task, especially while working with user input, file handling, and competitive programming problems. Sometimes we need to convert an integer into a string for concatenation, formatting, or digit manipulation.


Output
123

Explanation: The to_string() function converts the integer 123 into the string "123".

Common Method to Converting Number to String

C++ provides multiple methods to convert a numeric value into a string. Some methods are modern and simple, while others are useful for older compilers or special formatting needs.

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(data_type value);

  • Parameters: value -> Any numeric value such as int, float, double, or long.
  • Return Type: Returns a string object representing the numeric value.

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.

Syntax:

stringstream_object << value;
string_variable = stringstream_object.str();

Parameters

  • value -> Numeric value inserted into the stream.

Return Type

  • The str() function returns the converted string.

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.

Syntax:

sprintf(char_array, "format_specifier", value);

Parameters

  • char_array -> Character buffer where output will be stored.
  • "format_specifier" -> Specifies the data type format such as %d, %f.
  • value -> Numeric value to convert.

Return Type

  • Returns the number of characters written into the buffer.

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). 

 Syntax:

lexical_cast<string>(value);

Parameters

  • value -> Numeric value to be converted into a string.

Return Type

  • Returns the converted string value.

Output
The float value in string is : 10.5
The int value in string is : 17
  • Time Complexity: O(n)
  • Auxiliary Space: O(n)

Comparison of Methods

MethodAdvantagesDisadvantages
to_string()Simple and fastRequires C++11 or later
stringstreamFlexible and safeSlightly slower
sprintf()Works in older C/C++Less type-safe
boost::lexical_castEasy conversionRequires Boost library
Comment