iomanip
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, such as alignment, text formatting, and numeric precision in the console for better data visualization. In this Answer, we will explain several functions (illustrated below) along with their code for better understanding.
Setw
functionSetw stands for "set width". This function 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. setw
takes int
as an input.
#include <iostream>#include <iomanip>using namespace std;int main() {int num1 = 123;int num2 = 4567;int num3 = 89;cout<<"Output of the numbers : \n";cout << num1 << num2 << num3 << endl;cout<<"Output of the numbers by using setw : \n";cout << setw(8) << num1 << setw(8) << num2 << setw(8) << num3 << endl;return 0;}
By running the above code, you understand how it beautifies the output by adding spaces. If we don't use setw
, it will result in a wrong impression to the user.
setprecision
functionThis function sets the precision or the number of decimal places to be displayed when outputting a floating-point number. It can also round off the last displayed digit it needs. The parameter defines how many digits are to be displayed in the output. You can run this function by changing the parameter passed to setprecision()
. This function is mainly used in displaying significant digits.
#include <iostream>#include <iomanip>using namespace std;int main(){double pi = 3.14159265359;cout << setprecision(3) << pi << endl;return 0;}
fixed
functionTo display the decimal number in fixed-point notation, we use the fixed
function provided in the iomanip
library. The primary purpose of the fixed
function is to enforce a consistent number of decimal places irrespective of their actual precision.
#include <iostream>#include <iomanip>#include<iostream>using namespace std;int main(){double pi = 3.14159265359;cout << fixed << setprecision(2) << pi << endl;return 0;}
scientific
functionIn scientific notation, we can express a number that is too large or small in the power of 10. Iomanip
has the scientific
function that converts an output field into a readable form in the power of 10.
Below is the code that shows how the scientific
function converts a number to an exponential form so the user can read and interpret it easily.
#include <iostream>#include <iomanip>using namespace std;int main() {double num1 = 5325983729;double num2 = 0.00000053257;cout << scientific << setprecision(3) << num1 << endl;cout << scientific << setprecision(3) << num2 << endl;return 0;}
left
functionThe left
manipulator in iomanip
sets the output of the field to the left side and displays any additional width specified by the setw
as spaces to the right. You can run and test the below code by passing different parameters to the setw
function so that you can analyze the working of the left
operation.
#include <iostream>#include <iomanip>#include <iostream>using namespace std;int main(){cout << setw(15) << left << "Left-aligned:";cout << setw(12) << left << "Hello";return 0;}
right
functionThe right
function sets the output field of the text to the right side by adding spaces to the left as defined by the setw
. Have a look at its code.
#include <iostream>#include <iomanip>using namespace std;int main(){cout << setw(15)<<right << "Right-aligned:";cout <<setw(10) << right << "World" << endl;return 0;}
setfill
functionThe setfill
function is used to set the fill character when the width of the content displayed is smaller than the minimum defined by the setw
. It helps in creating a visually consistent and organized output in the console. You can pass any character to the setfill
function. In the below code, we pass the character '0'
in it. By default, the default character is a space ( ' '
) .
#include <iostream>#include <iomanip>usingint main() {int num = 42;cout << setfill('0') << setw(6) << num << endl;return 0;}
the iomanip
library in C++ is like a secret toolkit that adds magic to formatting and displaying data. It is the ultimate ally of developers that provides flexible manipulators and functions to fine-tune the appearance of output streams. With iomanip
, programmers can effortlessly create eye-catching displays that make their applications user-friendly, polished, and professional.
Which manipulator is used to set the width of the next output field to be exactly n characters wide?
Setprecision
Setfill
Setw
Hex
Ready to master C++?
With Learn C++ course, you’ll build practical skills from basic loops to complex program structures. Join now and start coding your way to success!
Free Resources