Printing with Field Widths and Precisions

The exact size of a field in which data is printed is specified by a field width. If the field width is larger than the data being printed, the data will be right justified within that field by default. (We demonstrate left justification in Section 28.10) The programmer inserts an integer representing the field width between the percent sign (%) and the conversion character (e.g., %4d) in the format specifier. Figure 28.12 prints two groups of five numbers each, right justifying those numbers that contain fewer digits than the field width. Note that the field width is increased to print values wider than the field and that the minus sign for a negative value uses one character position in the field. Also, if no field width is specified, the data prints in exactly as many positions as it needs. Field widths can be used with all format specifiers except the line separator (%n).

Figure 28.12. Right justifying integers in fields.

(This item is displayed on page 1337 in the print version)

1 // Fig. 28.12: FieldWidthTest.java 2 // Right justifying integers in fields. 3 4 public class FieldWidthTest 5 { 6 public static void main( String args[] ) 7 { 8 System.out.printf( "%4d ", 1 ); 9 System.out.printf( "%4d ", 12 ); 10 System.out.printf( "%4d ", 123 ); 11 System.out.printf( "%4d ", 1234 ); 12 System.out.printf( "%4d ", 12345 ); // data too large 13 14 System.out.printf( "%4d ", -1 ); 15 System.out.printf( "%4d ", -12 ); 16 System.out.printf( "%4d ", -123 ); 17 System.out.printf( "%4d ", -1234 ); // data too large 18 System.out.printf( "%4d ", -12345 ); // data too large 19 } // end main 20 } // end class RightJustifyTest  

1 12 123 1234 12345 -1 -12 -123 -1234 -12345  

Common Programming Error 28.3

Not providing a sufficiently large field width to handle a value to be printed can offset other data being printed and produce confusing outputs. Know your data!

Method printf also provides the ability to specify the precision with which data is printed. Precision has different meanings for different types. When used with floating-point conversion characters e and f, the precision is the number of digits that appear after the decimal point. When used with conversion character g, the precision is the maximum number of significant digits to be printed. When used with conversion character s, the precision is the maximum number of characters to be written from the string. To use precision, place between the percent sign and the conversion specifier a decimal point (.) followed by an integer representing the precision. Figure 28.13 demonstrates the use of precision in format strings. Note that when a floating-point value is printed with a precision smaller than the original number of decimal places in the value, the value is rounded. Also note that the format specifier %.3g indicates that the total number of digits used to display the floating-point value is 3. Because the value has three digits to the left of the decimal point, the value is rounded to the ones position.

Figure 28.13. Using precision for floating-point numbers and strings.

(This item is displayed on page 1338 in the print version)

1 // Fig. 28.13: PrecisionTest.java 2 // Using precision for floating-point numbers and strings. 3 public class PrecisionTest 4 { 5 public static void main( String args[] ) 6 { 7 double f = 123.94536; 8 String s = "Happy Birthday"; 9 10 System.out.printf( "Using precision for floating-point numbers " ); 11 System.out.printf( " %.3f %.3e %.3g ", f, f, f ); 12 13 System.out.printf( "Using precision for strings " ); 14 System.out.printf( " %.11s ", s ); 15 } // end main 16 } // end class PrecisionTest  

Using precision for floating-point numbers 123.945 1.239e+02 124 Using precision for strings Happy Birth  

The field width and the precision can be combined by placing the field width, followed by a decimal point, followed by a precision between the percent sign and the conversion character, as in the statement

printf( "%9.3f", 123.456789 );

which displays 123.457 with three digits to the right of the decimal point right justified in a nine-digit fieldthis number will be preceded in its field by two blanks.

Категории