0% found this document useful (0 votes)
8 views14 pages

3 formattedIO

Uploaded by

abhiparasharr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

3 formattedIO

Uploaded by

abhiparasharr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSCI 159-IOMANIP

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).

Examples: cout << 1.0; // Output: 1


cout << 1.234; // Output: 1.234
cout << 1.234567; // Output: 1.23457
• The last statement shows that the seventh digit is not simply truncated but rounded.

• Very large and very small numbers are displayed in exponential notation.
Example: cout << 1234567.8; // Output: 1.23457e+06
IOMANIP

• It is a library in C++ that is used to


manage input and output
formatting. It gives the user the
power to control output formatting
processes for better data
visualization

• Alignment

• Text formatting

• Numeric precision in the console


Formatting - setprecision
• This function is mainly used in defining the number of significant digits to be
displayed.
• Rounds off the last displayed digit if needed.
• Both the manipulator setprecision()and the method precision() can be
used to redefine precision to be used.

Example: cout << setprecision(3); // Precision: 3


// or: cout.precision(3);
cout << 12.34; // Output: 12.3
Cout << 1.234; //Output : 1.23
//Number of decimal places does not matter, significant digits
do
Fixed
• Helps to display the decimal number in fixed-point notation.
• Enforces a consistent number of decimal places irrespective of
their actual precision.
• By default, 6 digits after decimal
double x = 12.3456789;
cout << fixed << x; //Output : 12.345679

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

cout << setw(3) << x; //Ouput: 1.23457


//Width is set less than what is to be output, ignored
double x = 1.2345678888;
cout << " By default: " << x << endl; //Output : By default: 1.23457
cout << setw(30) << x;
showpoint, noshowpoint
• showpoint: Generates a decimal point character shown in floating-point output. The number of digits
after the decimal point corresponds to the used precision.

• 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

• To stop using it, you need to use noshowpos


• noshowpos: Generates non-negative numeric output without a + sign
(by default).
cout << noshowpos << 123; // Output: 123
Setfill

Sets a new character as the stream's fill character.

Example:
string s("spring flowers ");
cout << setfill(‘*') // Fill character is *
<< setw(20) << s ; // Field width 20

Outputs the string "spring flowers******".


Output Boolean Value

• 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.

Example: bool ok = true;


cout << ok << endl
// 1
cout << boolalpha << ok << endl;
// true

• To revert this setting using the noboolalpha manipulator.


Exercise

Formulate statements to perform the following:


a. Display the number 0.123456 in an output field with a width of 15. fill the blanks
with a star *
b. Output the number 23.987 as a fixed-point number rounded to two decimal
places, output in a field with a width of 12.
c. Output the number –123.456 as an exponential and with four decimal
spaces. How useful is a field width of 10?

You might also like