0% found this document useful (0 votes)
141 views2 pages

Format Specifications For Printf: Appendix

The document discusses the printf format method in Java. It specifies that printf takes a format string and variable number of arguments. The format string contains format specifiers that are replaced by the corresponding arguments during output. It lists common format specifiers like %d for integers, %f for floating point, and %s for strings. It also describes how number specifications before the dot set field width and numbers after the dot set decimal places. Adding a hyphen before the specifier left justifies the output instead of the default right justification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views2 pages

Format Specifications For Printf: Appendix

The document discusses the printf format method in Java. It specifies that printf takes a format string and variable number of arguments. The format string contains format specifiers that are replaced by the corresponding arguments during output. It lists common format specifiers like %d for integers, %f for floating point, and %s for strings. It also describes how number specifications before the dot set field width and numbers after the dot set decimal places. Adding a hyphen before the specifier left justifies the output instead of the default right justification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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.

You might also like