Using Flags in the printf Format String
Various flags may be used with method printf to supplement its output formatting capabilities. Seven flags are available for use in format strings (Fig. 28.14).
Flag |
Description |
---|---|
- (minus sign) |
Left justify the output within the specified field. |
+ (plus sign) |
Display a plus sign preceding positive values and a minus sign preceding negative values. |
space |
Print a space before a positive value not printed with the + flag. |
# |
Prefix 0 to the output value when used with the octal conversion character o. |
Prefix 0x to the output value when used with the hexadecimal conversion character x. |
|
0 (zero) |
Pad a field with leading zeros. |
, (comma) |
Use the locale-specific thousands separator (i.e., ',' for U.S. locale) to display decimal and floating-point numbers. |
( |
Enclose negative numbers in parentheses. |
To use a flag in a format string, place the flag immediately to the right of the percent sign. Several flags may be used in the same format specifier. Figure 28.15 demonstrates right justification and left justification of a string, an integer, a character and a floating-point number. Note that line 9 serves as a counting mechanism for the screen output.
Figure 28.15. Right justifying and left justifying values.
(This item is displayed on page 1339 in the print version)
1 // Fig. 28.15: MinusFlagTest.java 2 // Right justifying and left justifying values. 3 4 public class MinusFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.println( "Columns:" ); 9 System.out.println( "0123456789012345678901234567890123456789 " ); 10 System.out.printf( "%10s%10d%10c%10f ", "hello", 7, 'a', 1.23 ); 11 System.out.printf( 12 "%-10s%-10d%-10c%-10f ", "hello", 7, 'a', 1.23 ); 13 } // end main 14 } // end class MinusFlagTest
|
Figure 28.16 prints a positive number and a negative number, each with and without the + flag. Note that the minus sign is displayed in both cases, but the plus sign is displayed only when the + flag is used.
Figure 28.16. Printing numbers with and without the + flag.
1 // Fig. 28.16: PlusFlagTest.java 2 // Printing numbers with and without the + flag. 3 4 public class PlusFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%d %d ", 786, -786 ); 9 System.out.printf( "%+d %+d ", 786, -786 ); 10 } // end main 11 } // end class PlusFlagTest
|
Figure 28.17 prefixes a space to the positive number with the space flag. This is useful for aligning positive and negative numbers with the same number of digits. Note that the value -547 is not preceded by a space in the output because of its minus sign. Figure 28.18 uses the # flag to prefix 0 to the octal value and 0x to the hexadecimal value.
Figure 28.17. Using the space flag to print a space before non-negative values.
(This item is displayed on page 1340 in the print version)
1 // Fig. 28.17: SpaceFlagTest.java 2 // Printing a space before non-negative values. 3 4 public class SpaceFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "% d % d ", 547, -547 ); 9 } // end main 10 } // end class SpaceFlagTest
|
Figure 28.18. Using the # flag with conversion characters o and x.
(This item is displayed on page 1340 in the print version)
1 // Fig. 28.18: PoundFlagTest.java 2 // Using the # flag with conversion characters o and x. 3 4 public class PoundFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 int c = 31; // initialize c 9 10 System.out.printf( "%#o ", c ); 11 System.out.printf( "%#x ", c ); 12 } // end main 13 } // end class PoundFlagTest
|
Figure 28.19 combines the + flag, the 0 flag and the space flag to print 452 in a field of width 9 with a + sign and leading zeros, next prints 452 in a field of width 9 using only the 0 flag, then prints 452 in a field of width 9 using only the space flag.
Figure 28.19. Printing with the 0 (zero) flag fills in leading zeros.
(This item is displayed on pages 1340 - 1341 in the print version)
1 // Fig. 28.19: ZeroFlagTest.java 2 // Printing with the 0 (zero) flag fills in leading zeros. 3 4 public class ZeroFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%+09d ", 452 ); 9 System.out.printf( "%09d ", 452 ); 10 System.out.printf( "% 9d ", 452 ); 11 } // end main 12 } // end class ZeroFlagTest
|
Figure 28.20 use the comma (,) flag to display a decimal and a floating-point number with the thousands separator.Figure 28.21 encloses negative numbers in parentheses using the ( flag. Note that the value 50 is not enclosed in parentheses in the output because it is a positive number.
Figure 28.20. Using the comma (,) flag to display number with thousands separator.
(This item is displayed on page 1341 in the print version)
1 // Fig. 28.20: CommaFlagTest.java 2 // Using the comma (,) flag to display numbers with thousands separator. 3 4 public class CommaFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%,d ", 58625 ); 9 System.out.printf( "%,.2f", 58625.21 ); 10 System.out.printf( "%,.2f", 12345678.9 ); 11 } // end main 12 } // end class CommaFlagTest
|
Figure 28.21. Using the ( flag to place parentheses around negative numbers.
(This item is displayed on page 1341 in the print version)
1 // Fig. 28.21: ParenthesesFlagTest.java 2 // Using the ( flag to place parentheses around negative numbers. 3 4 public class ParenthesesFlagTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%(d ", 50 ); 9 System.out.printf( "%(d ", -50 ); 10 System.out.printf( "%(.1e ", -50.0 ); 11 } // end main 12 } // end class ParenthesesFlagTest
|