devxlogo

Converting a Number to a String

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<
See also  Is Machine Learning Automating Creativity in Graphic Design?
devxblackblue

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.

About Our Journalist