Java Reference
In-Depth Information
The options that you have for the various components of the format specification for a value are shown
in Table 8-10 .
TABLE 8-10 : Format Specification Options
OPTION DESCRIPTION
conversion This is a single character specifying how the argument is to be presented. The commonly used values
are the following:
'd' , 'o' , and 'x' apply to integer values and specify that the output representation of the value
should be decimal, octal, or hexadecimal, respectively.
'f' , 'g' , and 'a' apply to floating-point values and specify that the output representation should be
decimal notation, scientific notation (with an exponent), or hexadecimal with an exponent, respect-
ively.
'c' specifies that the argument value is a character and should be displayed as such.
's' specifies that the argument is a string.
'b' specifies that the argument is a boolean value, so it is output as "true" or "false" .
'h' specifies that the hashcode of the argument is to be output in hexadecimal form.
'n' specifies the platform line separator so "%n" has the same effect as "\n" .
argument_index This is a decimal integer that identifies one of the arguments that follow the format string by its se-
quence number, where "1$" refers to the first argument, "2$" refers to the second argument, and so
on. You can also use '<' in place of a sequence number followed by $ to indicate that the argument
should be the same as that of the previous format specification in the format string. Thus "<" speci-
fies that the format specification applies to the argument specified by the preceding format specifica-
tion in the format string.
This is a set of flag characters that modify the output format. The flag characters that are valid de-
pend on the conversion that is specified. The most used ones are the following:
'-' and '^' apply to anything and specify that the output should be left-justified and uppercase, re-
spectively.
'+' forces a sign to be output for numerical values.
'0' forces numerical values to be zero-padded.
flags
Specifies the field width for outputting the argument and represents the minimum number of charac-
ters to be written to the output.
width
This is used to restrict the output in some way depending on the conversion. Its primary use is to spe-
cify the number of digits of precision when outputting floating-point values.
precision
The best way to explain how you use this is through examples. I start with the simplest and work up from
there.
Formatting Numerical Data
I suggest that you set up a program source file with an empty version of main() into which you can plug a
succession of code fragments to try them out. You'll find TryFormattedOutput.java in the download with
the code fragments that are bold in the text commented out in main() . You can just comment out the one
you want to try but as always, it's better to type them in yourself.
The minimal format specification is a percent sign followed by a conversion specifier for the type of
value you want displayed. For example:
 
 
Search WWH ::




Custom Search