How to convert an int to a string in C++? (Also doubles, floats, etc…)

Here is how I do it. This procedure actually works for any type such as: int, double, float, etc…

Convert an Int Only

#include
#include
#include

using namespace std;

int main()
{
int i = 10;
string s = inToString(i);
return 0;
}

string intToString(int inInt)
{
stringstream ss;
string s;
ss << inInt; s = ss.str(); return s; } [/sourcecode] As a Template So It Works with All Types (int, float, double, etc…)

I found a comment on another guys blog that actually makes it work for any type such as double, float, etc.. It has this code using this template.
http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/

template
string anyTypeToString(const T& t)
{
std::stringstream ss;
ss << t; return ss.str(); } [/sourcecode] This works really well, I have been using it and it is so simple. Key words: int to string intToString double to string doublToString float to string floatToString

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) | Rhyous

  2. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  3. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  4. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  5. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  6. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  7. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  8. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  9. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  10. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  11. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  12. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  13. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  14. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  15. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  16. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  17. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  18. Pingback: How to convert an int to a character array when you don’t have access to the C++ Standard Library? « Rhyous's 127.0.0.1 or ::1

  19. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  20. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  21. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  22. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  23. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  24. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  25. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  26. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  27. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  28. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  29. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  30. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  31. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  32. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  33. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  34. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1

  35. Pingback: How to convert a string to an int in C++? (Or to a float, double, etc) « Rhyous's 127.0.0.1 or ::1