Sie sind auf Seite 1von 4

Using System.out.printf() or System.out.

format
The System.out.print() and System.out.println() have some limitations, like controlling the number
of decimal points that will be written on screen in the case of a real number output, or the number
of spaces before the output. These limitations are addressed by System.out.printf() method or the
System.out.format() method. These methods use conversion characters. The conversion
character specifies the data type of the variable or literal that will be output and the location of the
output.
Here is a basic example:

int i = 461012;
System.out.format("The value of i is: %d%n", i);

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent
newline character. The output is:

The value of i is: 461012

Table 10 shows the different conversion characters of Java.

Table 1: Java Conversion Characters

Conversion Type Description Example


Character
%d Decimal integer The result is formatted as a decimal integer 159
%X Hexadecimal integer The result is formatted as a hexadecimal integer 9f
%o Octal integer The result is formatted as an octal integer 237
%f Fixed-point floating- The result is formatted as a decimal number 15.9
point
%e Exponential floating- The result is formatted as a decimal number in 1.59e+01
point computerized scientific notation
%g General floating- The result is formatted using computerized —
point (the shorter of scientific notation or decimal format, depending on
e and f) the precision and the value after rounding.
%a Hexadecimal The result is formatted as a hexadecimal floating- 0x1.fccdp3
floating-point point number with a significand and an exponent
%s String The result is a Unicode character Hello
%c Character If the argument arg is null, then the result is H
"null". If arg implements Formattable, then
arg.formatTo is invoked. Otherwise, the result is
obtained by invoking arg.toString().
%b boolean If the argument arg is null, then the result is True
"false". If arg is a boolean or Boolean, then the
result is the string returned by String.valueOf().
Otherwise, the result is "true".
%h Hash code If the argument arg is null, then the result is 42628b2
"null". Otherwise, the result is obtained by
invoking Integer.toHexString(arg.hashCode()).
%tx Date and time Prefix for date and time conversion characters. Tue Feb 14
18:05:19 PST
2012
%% The percent symbol The result is a literal '%' ('\u0025') %

%n The platform- The result is the platform-specific line separator —


dependent line
2
Lesson 7 – Displaying Output on Screen

separator

Flags for printf


It is possible to specify flags that control the appearance of the formatted output.
For example, the comma flag adds group separators. That is,

System.out.printf("%,.2f", 10000.0 / 3.0);


prints 3,333.33

You can use multiple flags, for example, "%,(.2f", to use group separators and enclose
negative numbers in parentheses.

Table 2: Java printf Flags

Flags Purpose Example


+ Prints sign for positive and negative numbers +3333.33
space Adds a space before positive numbers | 3333.33|
0 Adds leading zeroes 003333.33
- Left-justifies field |3333.33 |
( Encloses negative number in parentheses (3333.33)
, Adds group separators 3,333.33
# (for f format) Always includes a decimal point 3,333.
# (for x or o format) Adds 0x or 0 prefix 0xcafe

$ Specifies the index of the argument to be formatted; for example, 159 9F


%1$d %1$x prints the first argument in decimal and hexadecimal

< Formats the same value as the previous specification; for example, 159 9F
%d %<x prints the same number in decimal and hexadecimal

Note: Printf Date and Time Conversion Characters are intentionally not included in this manual.
The following program illustrates the use of System.out.printf():

1 import java.util.Calendar;
2 public class PrintFormat {
3 public static void main(String[] args) {
4
3
Lesson 7 – Displaying Output on Screen

5 boolean isSeniorCitizen = true;


6 int age = 65;
7 char gender = 'M';
8 float f = 95.67F;
9 double pi = Math.PI;
10 int o = 987;
11 int hex = 532;
12
13 System.out.printf("Percent: %%\n",2);
14 System.out.printf("Boolean: %b\n", isSeniorCitizen);
15 System.out.printf("Integer: %d\n", age);
16 System.out.printf("Character: %c\n", gender);
17 System.out.printf("Floating point: %f\n", f);
18 System.out.printf("Double: %f\n", pi);
19 System.out.printf("Double: %5.2f\n", pi);
20 System.out.printf("Octal: %o\n", o);
21 System.out.printf("Hexadecimal: %x\n", hex);
22 System.out.printf("Local date: %tD\n", Calendar.getInstance());
23 System.out.printf("Local time: %tT\n", Calendar.getInstance());
24 }
25 }

The following is the output of the program except for the last two lines which is dependent on the
current computer date and time settings:
Percent: %
Boolean: true
Integer: 65
Character: M
Floating point: 95.669998
Double: 3.141593
Double: 3.14
Octal: 1733
Hexadecimal: 214
Local date: 05/08/12
Local time: 09:39:01

You may use System.out.format instead of System.out.printf and still have the same outpu
4
Lesson 7 – Displaying Output on Screen

Das könnte Ihnen auch gefallen