C++ iomanip Library
In this page:
#include <iomanip>
std::cout << std::setw(width) << value;
std::cout << std::setfill('fill_char') << std::setw(width) << value;
Setting Width with setw
setw(n) reserves a fixed-width field of n characters for the very next value printed, padding it with spaces so columns of output line up neatly.
It only affects the single next output operation, so you need to call it again before each value if you want a whole table of aligned columns.
Example: Setting Width with setw
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::setw(10) << "Name" << std::setw(10) << "Score" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Setting Fill Characters with setfill
setfill(c) changes what character fills the empty space setw leaves behind — spaces by default, but you might use 0 to zero-pad numbers or '*' for a visual separator.
It stays in effect for all subsequent setw calls until you change it again, unlike setw which only applies once.
Example: Setting Fill Characters with setfill
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::setfill('0') << std::setw(5) << 42 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Floating Point Precision
setprecision(n) caps how many significant digits (or decimal places, when combined with fixed) are shown for floating-point output, which prevents long, messy strings of digits like 3.14159265358979.
This is essential any time you're displaying computed values like averages or percentages to a user.
Example: Floating Point Precision
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare average and set it to 3.14159265358979
double average = 3.14159265358979;
// Print to the console with cout
std::cout << std::setprecision(4) << average << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left and Right Alignment
left and right control which side of a setw-defined field the text sticks to — left pushes text to the start with padding trailing after it, while right (the default) pads before the text so it aligns against the field's right edge.
Switching between them is what lets you build both left-aligned labels and right-aligned numbers in the same table.
Example: Left and Right Alignment
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << std::left << std::setw(10) << "Name" << std::endl;
// Print to the console with cout
std::cout << std::right << std::setw(10) << "Score" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Fixed Point Notation
Combining fixed with setprecision(2) locks floating-point output to exactly two decimal places regardless of the actual value's precision, which is exactly the format expected for displaying currency like $19.99.
Without fixed, setprecision alone controls total significant digits rather than decimal places specifically, which can produce unexpected results for values like 100.5.
Example: Fixed Point Notation
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.99
double price = 19.99;
// Print to the console with cout
std::cout << "$" << std::fixed << std::setprecision(2) << price << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting
#include <iomanip>, sosetwandsetprecisionare undefined. - Expecting
setwto persist, when it only applies to the next output item. - Using
setprecision(2)withoutfixedand getting significant digits instead of two decimal places.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: