devxlogo

Converting a Number to a String

The standard strstream family of classes enables you to convert numeric values into their string representation easily. You need to instantiate an strstream object, insert the numeric value to it using the overloaded << operator, and then extract the string from the strstream object using the str() member function. The following program converts a float variable to a string object using strstream:

 #include #include using namespace std;int main(){ ostrstream s; float f = 6.6777; s<

Note that strstream overload to << operator for all built-in types, e.g., int, float, double, char, pointers, etc. In the following example, a strstream object stores the value of a void pointer and then prints it on the screen:

 int main(){ ostrstream s; void *p = &s; // take address of s s<

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

©2025 Copyright DevX - All Rights Reserved. Registration or use of this site constitutes acceptance of our Terms of Service and Privacy Policy.