Java Reference
In-Depth Information
format specifier. The end of a format specifier is indicated by a conversion character
( f in our example).
Other possible format specifiers are described in Display 2.1. (A more complete list of
specifiers is given in Appendix 4.) The conversion character specifies the type of value
that is output in the specified format. Note that the first number specifies the total num-
ber of spaces used to output the value. If that number is larger than the number of spaces
needed for the output, extra blanks are added to the beginning of the value output. If
that number is smaller than the number of spaces needed for the output, enough extra
space is added to allow the value to be output; no matter what field width is specified,
printf uses enough space to fit in the entire value output. Both of the numbers in a for-
mat specifier such as %6.2f are optional. You may omit either or both numbers, in which
case Java chooses an appropriate default value or values. For example, %6f and %.2f .
Note that the dot goes with the second number. You can use a format specifier that is just
a % followed by a conversion character, such as %f or %g , in which case Java decides on the
format details for you. For example, the format specifier %f is equivalent to %.6f , mean-
ing six spaces after the decimal point and no extra space around the output.
The e and g format specifiers are partially explained in Display 2.1. We still need to
explain the meaning of the number after the decimal point in e and g format specifi-
ers, such as %8.3e and %8.3g . The first number, 8 in the examples, is the total field
width for the value output. The second number (the one after the decimal point) spec-
ifies the number of digits after the decimal point of the output. So the numbers, such
as 8.3 , have the same meaning in the f , e , and g formats.
The s and c formats, for strings and characters, may include one number that spec-
ifies the field width for outputting the value, such as %15s and %2c . If no number is
given, the value is output with no leading or trailing blank space.
When the value output does not fill the field width specified, blanks are added in
front of the value output. The output is then said to be right justified . If you add a
hyphen after the % , any extra blank space is placed after the value output, and the out-
put is said to be left justified . For example, the lines
conversion
character
e and g
s and c
right justified
left justified
double value = 12.123;
System.out.printf("Start%8.2fEnd", value);
System.out.println();
System.out.printf("Start%-8.2fEnd", value);
System.out.println();
produce the following output. The first line has three spaces before the 12.12 and the
second has three spaces after the 12.12 .
Start 12.12End
Start12.12 End
more
arguments
So far we have used printf to output only one value. However, printf can
output any number of values. The first argument always is a string known as the
Search WWH ::




Custom Search