Formatting a number as a string using stringstream class. - C++ Data Type

C++ examples for Data Type:Number Format

Description

Formatting a number as a string using stringstream class.

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;


int main() {/*from ww w.j  a  v a2s.  c om*/

   stringstream ss;

   ss << "There are " << 9 << " numbers.";
   cout << ss.str() << endl;  // stringstream::str() returns a string with the contents

   ss.str("");                   // Empty the string
   ss << showbase << hex << 16;  // Show the base in hexadecimal
   cout << "ss = " << ss.str() << endl;
   ss.str("");
   ss << 3.14;
   cout << "ss = " << ss.str() << endl;
}

Result


Related Tutorials