Java's formatted output is equivalent to String.Format, which is similar to C, such as
System.out.printf ("%8.2f", X);
In printf, you can use multiple parameters, such as:
System.out.printf ("Hello,%s. Next year, you'll be%d, name, age);
The conversion character for printf is as follows:
| Conversion character |
type |
Example |
| D |
Decimal integer |
159 |
| X |
hexadecimal integer |
9f |
| O |
Eight-binary integers |
237 |
| F |
Fixed-point floating point number |
15.9 |
| E |
Exponential floating-point number |
1.59e+01 |
| G |
Usual floating-point numbers |
|
| A |
Hexadecimal floating-point number |
0x1.fccdp3 |
| S |
String |
Hello |
| C |
Character |
H |
| B |
Boolean type |
TRue |
| H |
Hash code |
42628b2 |
| Tx |
Date Time |
See Time Introduction table |
| % |
Percent Sign |
% |
| N |
Separator |
|
In the printf function, you can use multiple flags, such as:
System.out.printf ("%,.2f", 10000.0/3.0);
Flags for printf are shown in the following table
| logo |
Purpose |
Example |
| + |
Symbols before printing a number |
+3333.33 |
| Space |
Add a space before a positive number |
| 3333.33| |
| 0 |
0 in front of the number |
003333.33 |
| - |
Align Left |
|3333.33 | |
| ( |
Negative numbers enclosed in parentheses |
(3333.33) |
| , |
Add a grouping separator |
3,333.33 |
| # (for F ) |
Include decimal points |
3,333. |
| # (for x or o) |
Add prefix 0x or 0 |
0xcafe |
| ^ |
Convert to uppercase |
0XCAFE |
| $ |
Specifies the format parameter index, such as%1$d,%1$d, that represents the decimal and hex print the first parameter |
159 9F |
| < |
Format the preceding parameters, such as%d%<x, in decimal and 16 To print the same parameter |
159 9F |
the conversion of the time date is as follows:
| Conversion character |
type |
Example |
| C |
Full Date and time |
Mon Feb 18:05:19 PST 2004 |
| F |
ISO 8601 Date |
2004-02-09 |
| D |
US time format (mm/dd/year) |
02/09/2004 |
| T |
24 Hour time |
18:05:19 |
| R |
12 Hour time |
06:05:19 pm |
| R |
24 Hours No second time |
18:05 |
| Y |
Four-year |
2004 |
| Y |
The latter two of the years |
04 |
| C |
The first two people of the year |
20 |
| B |
Full spelling of the month |
February |
| B or h |
Abbreviation of the Month |
Feb |
| M |
Two-month (pre-fill 0) |
02 |
| D |
Two-digit day (pre-complement 0) |
09 |
| E |
Date (not 0 before) |
9 |
| A |
Full day of the week |
Monday |
| A |
Abbreviation of the day of the week |
Mon |
| J |
The number of days of the year, three-bit complement 0 |
16X |
| H |
24-hour hour, two-bit complement 0 |
18 |
| K |
24-hour hours, two not 0 |
18 |
| I |
12-hour hour, two-bit complement 0 |
06 |
| L |
12-hour hours, two not 0 |
6 |
| M |
Minutes, two-bit complement 0 |
05 |
| S |
Seconds, two-bit complement 0 |
19 |
| L |
Milliseconds, three-bit complement 0 |
047 |
| N |
nanoseconds, nine-bit complement 0 |
047000000 |
| P |
Capitalize in the afternoon |
Pm |
| P |
Last Afternoon lowercase |
Pm |
| Z |
RFC 822 numeric offset from GMT |
-0800 |
| Z |
Time |
Pst |
| S |
1970-01-01 00:00:00 seconds |
1078884319 |
| E |
1970-01-01 00:00:00 milliseconds |
1078884319047 |
Java formatted output