3 formattedIO
3 formattedIO
Formatted Output
Standard setting
• Floating-points are displayed to six digits by default.
• Decimals are separated from the integral part of the number by a decimal point.
• Trailing zeroes behind the decimal point are not printed. If there are no digits after
the decimal point, the decimal point is not printed (by default).
• Very large and very small numbers are displayed in exponential notation.
Example: cout << 1234567.8; // Output: 1.23457e+06
IOMANIP
• Alignment
• Text formatting
double x = 1.23456789;
cout << fixed << x; //Output : 1.234568
Scientific
• By default, a number will be expressed in fixed notation.
• By default, a very large and small number will be expressed in scientific
notation as power of 10.
double x = 123456789.123456789;
cout << x; //Output : 1.23457e+08
• Iomanip has the scientific function that converts an output field into a
readable form in the power of 10.
double x = 12.3456789;
cout << scientific << x; //Output : 1.234568e+01
Combining setprecision, scientific/fixed
notations
double x = 1234567.8888;
cout << " By default: " << x << endl;
//Output: By default: 1.23457e+06
cout.precision(3);
cout << x;
//Output : 1.23e+06
------------------------------------------------------------------------------------
double x = 1.2345678888;
cout << " By default: " << x << endl;
//Output : By default: 1.23457
cout.precision(3);
cout << x;
//Output : 1.23
------------------------------------------------------------------------------------
Combining setprecision, scientific/fixed
notations
double x = 1234567.99998888;
cout << " By default: " << x << endl; //Output : By default: 1.23457e+06
cout.precision(3);
cout << scientific << x;
//Output : 1.235e+06
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
double x = 1.2345678888;
cout << " By default: " << x << endl; //Output : By default: 1.23457
cout.precision(3);
cout << fixed << x;
//Output : 1.235
setw
•Setw stands for "set width".
•Allows the user to define the minimum width of the following output field in a stream.
•It aligns data in columns and has a well-organized output to read.
•Unlike setprecision, fixed etc. setw may need to be repeatedly called to set the width
for multiple operations.
double x = 1.2345678888;
cout << " By default: " << x << endl;
//Output : By default: 1.23457
• noshowpoint: If there are no digits after the decimal point, the decimal point is not printed (by default)
• Example: cout << showpoint << 1.0; // Output: 1.00000 (6 significant digits)
double x = 12.0;
cout.precision(2); // Precision 2
cout << " By default: " << x << endl;
//Output : By default: 12
cout << " showpoint: " << showpoint << x << endl;
//Output : 12.
showpos
• Showpos: Generates a + sign in non-negative numeric
cout << showpos << 123; // Output: +123
The other positive numbers are printed with their sign as well:
cout << 22; // Output: +22
Example:
string s("spring flowers ");
cout << setfill(‘*') // Fill character is *
<< setw(20) << s ; // Field width 20
• By default the << operator outputs boolean values as integers, with the value 0
representing false and 1 true.
• If you need to output the strings true or false instead, the flag boolalpha must
be set.