SETW() & SETPRECISION() IN C++
We know we can print our result on the screen using cout
statement. It is fine if we are only interested in the result. But at times we
may need to print our output in a certain way. This task can be fulfilled by
using I/O manipulators like setw(), setprecision(), etc.
Formatting Output :
Formatting output is important in the development of output
screens, which can be easily read and understood. C++ offers several
input/output manipulators. Two of these I/O manipulators are setw() and setprecision().
In order to use these manipulators, you must include header file iomanip,
i.e. your program should also have following #include directive along with
other #include directives :
#include<iomanip>
note : if you are using Turbo C, you
need to use #include<iomanip.h>
setw() manipulator :
The setw() manipulator sets the width of the field
assigned for output. It basically right justifies the output. It takes the size
of the field (in number of characters) as a parameter. For example, the code :
cout<<setw(6)<<”R”;
generates the following output (each underscore is for a
space)
Output : _ _ _ _ _ R
setw() manipulator is non-sticky
statement. This means that a setw() manipulator is applicable only
to its immediately following statement. So, if you want to right justify three
numbers in separate lines, you need to repeat setw() manipulators for
each line. For example,
cout<<setw(8)<<6<<endl;
cout<<setw(8)<<66<<endl;
cout<<setw(8)<<666;
Output: _ _ _ _ _ _ _ 6
_ _ _ _ _ _ 6 6
_ _ _ _ _ 6 6 6
Also,
cout<<setw(6)<<6<<
“R\n”;
cout<<setw(6)<<6;
cout<<setw(6)<<
“R”;
Output : _ _ _ _ _ 6 R
_ _ _ _ _ 6 _ _ _ _ _ R
note
: In all the above outputs, underscore (_) means a blank space.
setprecision() manipulator :
The setprecission()
manipulator sets the total number of digits to be displayed when floating point
numbers are printed. For example,
cout<<
setprecision(5)<<123.456;
Output : 123.46
(note:
See it displays 5 digits and rounds off the floating point number)
setprecission() manipulator can also be used to set
the number of digits after decimal by using an ios flag. An ios flag
can be set using setf() as :
cout.setf(ios::fixed);
Once the flag has been
set, the number we pass as argument in setprecision() is the number of
decimal places we want to display. For example,
cout.setf(ios::fixed);
cout<<setprecision(3)<<123.4567;
Output : 123.456
(See
it displays 5 digits after decimal without rounding off).
Also, setprecision()
is a sticky manipulator, i.e. whatever precision we set once, sticks
with cout until we change it with another setprecision() later in
the program.
Additional ios
flags :
General syntax:
cout.setf(ios::flag);
where flag is any one
of the following :
Few examples :
If you want to
represent 6.8 as 6.80
cout.setf(ios::showpoint);
cout<<setprecision(3)<<6.8;
Output : 6.80
If you want to
represent 60 as +60
cout.setf(ios::showpos);
cout<<60;
Output : +60
Any flag set using setf()
can be removed using unsetf(). For example,
cout.setf(ios::showpos);
cout<<60<<endl;
cout<<25<<endl;
cout.unsetf(ios::showpos);
cout<<60;
Output : +60
+25
60
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download the pdf form of this article here.
Post a Comment