Issuu on Google+

#include <iostream> #include <iomanip> using namespace std; void main() { int a=10,b=250,c=7658000; double d=24.56,e=4.7856,f=1234.456; cout<<endl<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl; cout<<"\na,b,c without format : "<<a<<b<<c<<endl; cout<<"\na,b,c with format setw(10) : "<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<endl<<endl; cout<<endl<<"\nd = "<<d<<" e = "<<e<<" f = "<<f; cout<<"\n\nd,e,f without format : "<<d<<e<<f; cout<<"\n\nd,e,f with format setw(10) : "<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f left justified, setw(10) : "<<setiosflags(ios::left)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f right justified, setw(10) : "<<setiosflags(ios::right)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f<<endl<<endl; cout<<"\n\nd,e,f setprecision(2), setw(10) : "<<setprecision(2)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f setprecision(3), setw(10) : "<<setprecision(3)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f fixed, setprecision(2), setw(10) : "<<setiosflags(ios::fixed)<<setprecision(2)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f fixed, setprecision(4), setw(10) : "<<setiosflags(ios::fixed)<<setprecision(4)<<setw(10)<<d<<setw(10)<<e<<setw(10)<<f; cout<<"\n\nd,e,f fixed, setprecision(4), setfill('x'), setw(10) : \n"<<setiosflags(ios::fixed)<<setprecision(4)<<setfill('x')<<setw(10)<<d<<endl<<setw(10)<<e<<endl<<setw(10) <<f;

}

cout<<endl<<endl;


OUTPUT a = 10 b = 250 c = 7658000 a,b,c without format : 102507658000 a,b,c with format setw(10) :

10

250

7658000

d = 24.56 e = 4.7856 f = 1234.46 d,e,f without format : 24.564.78561234.46 d,e,f with format setw(10) :

24.56

d,e,f left justified, setw(10) : 24.56 d,e,f right justified, setw(10) :

4.7856 4.7856

24.56

1234.46 1234.46

4.7856

d,e,f setprecision(2), setw(10) :

25

d,e,f setprecision(3), setw(10) :

24.6

4.8 1.2e+003 4.79 1.23e+003

d,e,f fixed, setprecision(2), setw(10) :

24.56

d,e,f fixed, setprecision(4), setw(10) :

24.5600

d,e,f fixed, setprecision(4), setfill('x'), setw(10) : xxx24.5600 xxxx4.7856 x1234.4560 Press any key to continue . . .

1234.46

4.79

1234.46

4.7856 1234.4560


Output formatting in C++