Java Reference
In-Depth Information
The format string contains normal text together with format specifiers
that tell the formatter how you want the following values to be format-
ted. For example, you can print the value of Math.PI to three decimal
places using
System.out.printf("The value of Math.PI is %.3f %n",
Math.PI);
which prints
The value of Math.PI is 3.142
A format specifier starts with a % character and ends with a character
that indicates the type of conversion to perform. In the example above,
the f conversion means that the argument is expected to be a floating-
point value and that it should be formatted in the normal decimal
format. In contrast, an e conversion is a floating-point conversion that
produces output in scientific notation (such as 3.142e+00 ). Other conver-
sions include ' d for integers in decimal format, x for integers in hexa-
decimal, and s for strings or general object conversions. Conversion in-
dicators that can produce non-digit text are defined in both a lowercase
and uppercase form, such as e and E , where the uppercase form indic-
ates that all text in the output will be converted to uppercase (as if the
resulting String had toUpperCase invoked on it).
In addition to the conversion indicator, a format specifier can contain
other values that control the layout of the converted value. In the ex-
ample above the ".3" is a precision indicator, which for a floating-point
decimal conversion indicates how many decimal places should appear
in the result, with the value rounded up or down as appropriate. You
can also control the width of the output text, ensuring that different
formatted elements line up correctly (such as for printing data in a tab-
ular form) regardless of the actual value being formatted. There are also
flags that can be specified to control things like the justification (left or
right) and the padding character to use to maintain the minimum width.
 
Search WWH ::




Custom Search