How To Convert a Number To std::string

There are many methods available. You can use itoa, ltoa etc. to convert numbers to char*. However the following method using stringstream can convert any type of number to std::string.


#include <sstream>
using namespace std;
int main()
{
   int No = 140;  // Or you can use float No = 10.5f
   stringstream StrStream;
   StrStream << No;
   string MyString = StrStream.str();
   return 0;
}


The resulting MyString contains the value of No as string.

Click to see how to convert string to number

Click to see how to convert CString to std::string

One Response to “How To Convert a Number To std::string”

  1. How to Convert string to Number « My Experiments with C++ Says:

    [...] Click to see how to convert number to string [...]

Leave a Reply