Java Reference
In-Depth Information
The exact meaning of precision, width, and flags depends on the con-
version being applied.
There are two special conversions that we quickly mention. The first is
the
%
conversion used to output the
%
character. Because the
%
charac-
ter marks the start of a format specifier, you need a way to include a
%
character that should actually appear in the output. The format specifier
%%
will do just that (just as the escape character
\\
is used to produce
the single character
\
). You can specify a width with this conversion to
pad the output with spaces: on the left if no flags are given, and on the
right if the flag is given to request left-justification.
The second special conversion is the line separator conversion
n
. A
format specifier of
%n
(as used in the example) outputs the platform spe-
cific line separator. The line separator string is defined by the system
property
line.separator
and is not necessarily a single newline character
the line separator for you,
printf
and
format
require that you remember
to do this yourself. No width, precision, or flag values can be used with
the line separator conversion.
The following sections look at the format specifiers for numeric, charac-
ter, and general conversions. The formatting conversions for dates and
times are discussed in "
Using Formatter with Dates and Times
"
on page
java.math.BigInteger
and
java.math.BigDecimal
classes, but those are not
discussed hereconsult the
Formatter
class documentation for information
concerning those classes.
22.1.1. Format Specifiers
The general form of a format specifier for general, character, or numeric
conversions is
%[argument_index][flags][width][.precision]conversion