Page 209 - Introduction to Programming with Java: A Problem Solving Approach
P. 209

                5.7 Formatted Output with the printf Method 175
Precision and Width
The precision part of a format specifier works in conjunction with the f and e conversion characters; that is, it works with floating-point data items. It specifies the number of digits that are to be printed to the right of the decimal point. We’ll refer to those digits as the fractional digits. If the data item has more fractional digits than the precision’s value, then rounding occurs. If the data item has fewer fractional digits than the precision’s value, then zeros are added at the right so the printed value has the specified number of fractional digits.
The width part of a format specifier specifies the minimum number of characters that are to be printed. If the data item contains more than the specified number of characters, then all of the characters are printed. If the data item contains fewer than the specified number of characters, then spaces are added. By default, output values are right aligned, so when spaces are added, they go on the left side.
Note this code fragment and its associated output:
System.out.printf("Cows are %6s\n", "cool");
System.out.printf("But dogs %2s\n", "rule");
System.out.printf("PI = %7.4f\n", Math.PI);
Ouput
6 characters
        Cows are cool
But dogs rule
PI = 3.1416
7 characters
          In the third statement above, note the %7.4f specifier. It’s easy to get fooled by the 7.4. It looks like it might be saying “seven places to the left of the decimal point and four places to the right of the deci- mal point,” but it’s actuallAy spayaing“osevenPtoDtalFspaceEs,nwihthafounr cplaecesrto the right of the decimal point.” And don’t forget that the decimal point is counted as one of those seven total spaces. Math.PI’s value is 3.141592653589793, and when it gets printed with four places to the right of the decimal point, it gets rounded to 3.1416.
Flags
As a refresher, here’s the syntax for a format specifier:
% [ fl a g s ][ w i d t h ][ . p r e c i s i o n ] c o n v e r s i o n - c h a r a c t e r
We’ve described the conversion, precision, and width parts of a format specifier. It’s now time to dis- cuss flags. Flags allow you to add supplemental formatting features, one flag character for each formatting feature. Here’s a partial list of flag characters:
-
Display the printed value using left justification.
If a numeric data item contains fewer characters than the width specifier’s value, then pad the printed value with leading zeros (i.e., display zeros at the left of the number).
Display a numeric data item with locale-specific grouping separators. In the United States, that means commas are inserted between every third digit at the left of the decimal point.
Display a negative numeric data item using parentheses, rather than using a minus sign. Using parentheses for negative numbers is a common practice in the field of accounting.
0
,
(
Let’s see how format specifiers work in the context of a complete program. See Figure 5.10’s BudgetReport program. Note that we use the same format string for printing the column headers and the column under- lines, and the format string is stored in a HEADING_FMT_STR named constant. If you use a format string







































































   207   208   209   210   211