/** * precision.cpp * * What happens when we format numbers * * Written by: Simon Parsons * Last modified: 16th October * **/ #include using namespace std; int main(){ double pi = 3.1415927; double small = 0.000235; // No restriction on precision cout << endl; cout << "No restriction on precision" << endl; cout << pi << endl; cout << small << endl; // Now make precision 8 cout << endl; cout << "Now make precision 8" << endl; cout.precision(8); cout << pi << endl; cout.precision(8); cout << small << endl; // Now make precision 2. This prints the specified number // of significant figures --- that is it ignores leading zeros. cout << endl; cout << "Now make precision 2" << endl; cout.precision(2); cout << pi << endl; cout.precision(2); cout << small << endl; // Now make precision 3 and fixed. The ios::fixed // forces all the output to have exactly the number of // digits after the decimal point that are set by precision(). cout << endl; cout << "Now make precision 2 and ios::fixed" << endl; cout.precision(2); cout.setf(ios::fixed); cout << pi << endl; cout.setf(ios::fixed); cout.precision(2); cout << small << endl; cout << endl; return 0; }