Java Reference
In-Depth Information
It follows that when outputting the format string, the format specifiers are replaced with
the formatted values of the corresponding arguments.
A format specifier for general, character, and numeric types has the following syntax:
%[argument_index$][flags][width][.precision]conversion
3
The expressions in square brackets are optional. That is, they may or may not appear in a
format specifier.
The option argument_index is a (decimal) integer indicating the position of the
argument in the argument list. The first argument is referenced by "1$" ,thesecond
by "2$" , and so on.
The option flags is a set of characters that modifies the output format. The set of valid flags
depends on the conversion.
The option width is a (decimal) integer indicating the minimum number of characters to
be written to the output.
The option precision is a (decimal) integer usually used to restrict the number of characters.
The specific behavior depends on the conversion.
The required conversion is a character indicating how the argument should be formatted.
The set of valid conversions for a given argument depends on the argument's data type.
Table 3-2 summarizes some of the supported conversions.
TABLE 3-2 Some of Java's Supported Conversions
's'
general
The result is a string
'c'
character
The result is a Unicode character
'd'
integral
The result is formatted as a (decimal) integer
The result is formatted as a decimal number in
computerized scientific notation
'e'
floating point
'f'
floating point
The result is formatted as a decimal number
'%'
The result is '%'
percent
'n'
line separator
The result is the platform-specific line separator
The method printf is available in Java 5.0 and higher versions.
The following example shows how the conversions f and e work to output floating-
point numbers in fixed decimal and scientific formats.
Search WWH ::




Custom Search