Anda di halaman 1dari 2

Format Specifications for printf 4

SYNTAX

System.out.printf(Format_String, Output_1, Output_2, ..., Output_Last);

Format_String is a string including one format specifier for each Output argument.
Format_String is output with each format specifier replaced by its corresponding Output
APPENDIX
argument in the format given by the Output arguments format specifier.

Display A4.1 Format Specifiers for System.out.printf

CONVERSION TYPE OF OUTPUT. EXAMPLES


CHARACTER
d Decimal (ordinary) integer. %5d
%d

f Fixed-point (everyday notation) floating-point. %6.2f


%f

e E-notation floating-point. %8.3e


%e

g General floating-point. (Java decides %8.3g


whether to use E-notation or not.) %g

s String. %12s
%s

c Character. %2c
%c

b Boolean. The corresponding Output argument is a Boolean %6b


expression. Outputs true or false. %b

n Denotes a line break. This does not correspond to an Output %n


argument. It is approximately equivalent to \n.

A number of the form N.M in a format specifier specifies a field width of N spaces
with M digits after the decimal point. If only one number N is given, it specifies a field
width, and if there is a decimal point in the output, then the number of digits after the
decimal point is determined by Java.
1128 APPENDIX 4 Format Specifications for printf

When the value output does not fill the field width specified, then 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 %, then any extra blank space is placed after the value output and
the output is said to be left justified. For example, %8.2f is right justified and %-8.2f is
left justified.

Anda mungkin juga menyukai