Demonstrate the numeric format flags. - C++ File Stream

C++ examples for File Stream:cout

Description

Demonstrate the numeric format flags.

Demo Code

#include <iostream>
using namespace std;
int main()/*w  w  w  .  ja  v  a2s  .  c om*/
{
   int x = 100;
   double f = 98.6;
   double f2 = 123456.1230;
   double f3 = 1234567.0;
   cout.setf(ios::hex, ios::basefield);
   cout << "x in hexadecimal: " << x << endl;
   cout.setf(ios::oct, ios::basefield);
   cout << "x in octal: " << x << endl;
   cout.setf(ios::dec, ios::basefield);
   cout << "x in decimal: " << x << "\n\n";
   cout << "f, f2, and f3 in default floating-point format:\n";
   cout << "f: " << f << "  f2: " << f2 << "  f3: " << f3 << endl;
   cout.setf(ios::scientific, ios::floatfield);
   cout << "After setting scientific flag:\n";
   cout << "f: " << f << "  f2: " << f2 << "  f3: " << f3 << endl;
   cout.setf(ios::fixed, ios::floatfield);
   cout << "After setting fixed flag:\n";
   cout << "f: " << f << "  f2: " << f2 << "  f3: " << f3 << "\n\n";
   // Return to default floating-point format.
   cout << "Returning to default floating-point format.\n";
   cout.unsetf(ios::fixed);
   cout << "f2 in default format: " << f2 << "\n\n";
   // Set the showpoint flag.
   cout << "Setting showpoint flag.\n";
   cout.setf(ios::showpoint);
   cout << "f2 with showpoint set: " << f2 << "\n\n";
   cout << "Clearing the showpoint flag.\n\n";
   cout.unsetf(ios::showpoint);
   // Set the showpos flag.
   cout.setf(ios::showpos);
   cout << "Setting showpos flag.\n";
   cout << "x in decimal after setting showpos: " << x << endl;
   cout << "f in default notation after setting showpos: " << f << "\n\n";
   // Set the uppercase flag.
   cout << "Setting uppercase flag.\n";
   cout.setf(ios::uppercase);
   cout << "f3 with uppercase flag set: " << f3 << endl;
   return 0;
}

Result


Related Tutorials