C++ stringstream setprecision()

Description

C++ stringstream setprecision()

#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <ios> 


using namespace std; 

int main(int argc, char *argv [])
{   //  ww w  . j  ava 2 s  .co  m

        stringstream myStringStream; 
  
        myStringStream << "Hi" << endl; 
  
        cout << "width: " << myStringStream.width() << endl; 
        myStringStream << 123 << endl; 
        myStringStream << setw(10) << 123 << endl; 
        myStringStream << setw(0) << 123 << endl; 
  
        myStringStream << 123.567 << endl; 
        myStringStream << setprecision(4) << 123.567 << endl; 
        myStringStream << fixed << setprecision(2) << 123.567 << endl; 
        myStringStream << defaultfloat << setprecision(0) << 123.567 << endl; 
  
        myStringStream << 110 << endl << showbase; 
        myStringStream << hex << 110 << endl; 
        myStringStream << oct << 110 << endl; 
        myStringStream << dec << 110 << endl << noshowbase; 
  
        string myString = myStringStream.str(); 
        cout << myString; 
        return 0; 
}



PreviousNext

Related