Skip to content

Latest commit

 

History

History
156 lines (111 loc) · 5.44 KB

formatting.md

File metadata and controls

156 lines (111 loc) · 5.44 KB

What's News

Archaelogists have uncovered ancient markings that indicate a picture really is not worth one thousand words. The prehistoric writing indicates, however, that formatting does matter.

Formatting

C++ has a bevy of functions available to make it easy to generate nicely formatted output. You will rarely use them in industry, but it is good to practice using them and know that they exist. To repeat, it is important that you know that these functions exist, but it is not important that you memorize their semantics. These are functions whose documentation is easily accessible for situations where you need to use them. To use these functions, you will need to include the iomanip header file.

Set the (minimum) width

There are situations where you want to print the value of a variable but you want to pad that output so that it is at least a certain width. setw is the function that will do that. setw takes a single parameter -- the minimum width of the next item output. A picture is worth a thousand words:

1 #include <iomanip>
2 #include <iostream>
3 #include <string>
4 
5 int main() {
6   int one{1};
7   std::cout << "-" << std::setw(25) << one << "-\\n";
8 }

will print

-                      1-

Because 1 takes a single character to print, there are 24 empty spaces printed so that the minimum width (25) is achieved. Note that this is the minimum -- if the contents of the variable one had taken more than 25 characters to print, the value would not have been truncated (or clipped).

The value set in a call to setw persists only for the very next output. As soon as a value is output, the minimum width goes back to the default. In other words, the value set in a call to setw does not stick.

Make setw Perform Tricks

In the previous example, the extra space required to meet the minimum width was filled with spaces. We can change that and choose any character to fill those extra spaces. Use the setfill function to do so.

 1 #include <iomanip>
 2 #include <iostream>
 3 #include <string>
 4 
 5 int main() {
 6   int one{1};
 7 
 8   std::cout << std::setfill('@');
 9   std::cout << "-" << std::setw(25) << one << "-\n";
10 }

will print

-@@@@@@@@@@@@@@@@@@@@@@@@1-

The fill value set with a call to setfill does stick.

Everything you own in a box to the left

So far all we've been able to do is print the content on the right-hand side. But, you can left-justify the content as well! Just use the left function.

 1 #include <iomanip>
 2 #include <iostream>
 3 #include <string>
 4 
 5 int main() {
 6   int one{1};
 7 
 8   std::cout << std::setfill('@') << std::left;
 9   std::cout << "-" << std::setw(25) << one << "-\n";
10 }

prints

-1@@@@@@@@@@@@@@@@@@@@@@@@-

The fill value set using setfill sticks.

Set the number of significant digits

Use setprecision to set the number of significant digits when printing numbers.

1 #include <iomanip>
2 #include <iostream>
3 #include <string>
4 
5 int main() {
6   double dbl = 6.45;
7   std::cout << "dbl: " << std::setprecision(2) << dbl << "\n";
8 }

will print

dbl: 6.5

Note that setprecision will cause rounding!

1 or 1.0?

We have seen that std::cout helpfully refuses to print the .0 when a floating point number has no fractional part. We can change this behavior using the showpoint function.

1 #include <iomanip>
2 #include <iostream>
3 #include <string>
4 
5 int main() {
6   double dbl = 6.0;
7   std::cout << "dbl: " << std::showpoint << dbl << "\n";
8 }

will print

6.00000

Setprecision is Finicky

setprecision will not always do what you want. If you have a very large number it will sometimes convert it to scientific notation. Again, don't worry about the specifics -- you can always look up the behavior in the documentation when you need it! However, we can combine setprecision with fixed to get more reliable behavior. When combined with fixed, setprecision can be used to specify the number of significant digits printed after the ..

1 #include <iomanip>
2 #include <iostream>
3 #include <string>
4 
5 int main() {
6   double dbl = 6.123456;
7   std::cout << "dbl: " << std::fixed << std::setprecision(4) << dbl << "\n";
8 }

will print

dbl: 6.1235

Again, note the rounding! Using fixed in combination with setprecision is a good way to print dollars and cents.