#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{long m=56, n=-1;
double x=12.56,y = .123456789e-4;
cout<<"12345678901234567890123456789012345678901234567890" <<endl;
//six significant figures by default are displayed
cout<<"six significant figures by default are displayed; values run together\n";
cout<<x;
cout<<y;
cout<<endl;
//setw establishes a field width for the value immediately following it
cout
<<"setw establishes a field width for the value immediately following it\n";
cout<<setw(10)<<x;
cout<<setw(20)<<y ;
cout<<endl;
//setprecision establishes the precision until it is changed.
//setprecision by itself sets the total number of digits to be displayed
cout<<"setprecision of 8 sets total number of digits to be displayed\n";
cout<<setprecision(8)<<x<<'\n';
cout<<y ; //setprecision of 8 still in play
cout<<endl;
cout<<"setprecision of 4\n";
cout<<setw(20)<<setprecision(4)<<x<<'\n';
cout<<setw(20)<<y ; //setprecision of 4 still in play
cout<<endl;
cout<<endl;
//the exact format can be controled by using setiosflags.
//it applies until changed.
//the number of decimal places to be printed can be set by using
//setiosflags(ios::fixed) in conjunction with setprecision.
//together the meaning of setprecion is to set the number of decimal
//places to be printed.
cout<<"the exact format can be controled by using setiosflags(ios::fixed)\n";
cout <<"setprecision of 5 now in play"<<endl;
cout<<setw(20)<<setprecision(5)<< setiosflags(ios::fixed)<<x<<'\n';
cout<<setw(20)<<y ; //setprecision of 5 still in play
cout<<endl;
cout<<endl;
cout << "or \n";
cout<<setw(20)<< fixed <<x<<'\n';
cout<<endl;
cout<<endl;
//you can return to scientific format when you want
cout<<
"return to scientific when you want\nprecison here has returned to default\n";
cout<<setw(20)<< scientific <<y<<'\n';
// print integers
cout<<"print integers\n";
cout<<setw(10)<< n <<'\n';
cout<<setw(10)<< m <<'\n';
// print integers with justification
cout<<" print integers with justification\n";
cout<<setw(10)<< setiosflags(ios::left) <<n <<'\n';
cout<<setw(10)<< right<< m <<'\n';
//you can find out the amount of storage associated with a given type
cout << "the sizeof(long) is "<< sizeof(long)<<endl;
// you can get integers printed in alternate bases.
cout <<"m is "<< m << " ; m in hex is "<< hex<< setw(20) << m <<endl;
cout <<dec <<"n is "<< n << " ; n in hex is "<< hex<< setw(20) << n <<endl;
cout<<" m and n in octal are ";
cout << oct<< setw(20) << m << setw(20)<< n;
cout<<endl;
return 0;
}
[pt@cs aix]$ c++ cout2.cpp
[pt@cs aix]$ a.out
12345678901234567890123456789012345678901234567890
six significant figures by default are displayed; values run together
12.561.23457e-05
setw establishes a field width for the value immediately following it
12.56 1.23457e-05
setprecision of 8 sets total number of digits to be displayed
12.56
1.2345679e-05
setprecision of 4
12.56
1.235e-05
the exact format can be controled by using setiosflags(ios::fixed)
setprecision of 5 now in play
12.56000
0.00001
or
12.56000
return to scientific when you want
precison here has returned to default
1.23457e-05
print integers
-1
56
print integers with justification
-1
56
the sizeof(long) is 4
m is 56 ; m in hex is 38
n is -1 ; n in hex is ffffffff
m and n in octal are 70 37777777777
No comments:
Post a Comment