Alignment output in Chinese and English
The console output is as follows:
Forest dog
In fact, alignment is required for the output, and various types of search for the formatting output of java can be achieved with a simple "\ t.
The Code is as follows:
System. out. printf ("abcefg \ t" "def \ t" "king of the forest \ t" "A dog \ t ");
Ii. knowledge points on java formatting and Output
The printf method of class c 2.1
JavaFormatted output is equivalent to String. Format, which is very similar to C, for exampleSystem. out. printf ("% 8.2f", x );
In printf, multiple parameters can be used, such as: System. out. printf ("Hello, % s. Next year, you'll be % d", name, age );
The conversion Characters Used for printf are as follows:
|
Conversion character |
Type |
Example |
|
D |
Decimal integer |
159 |
|
X |
Hexadecimal integer |
9f |
|
O |
Octal integer |
237 |
|
F |
Fixed Point floating point number |
15.9 |
|
E |
Exponential floating point number |
1.59e + 01 |
|
G |
Floating Point Number |
|
|
A |
Hexadecimal floating point number |
0x1. fccdp3 |
|
S |
String |
Hello |
|
C |
Character |
H |
|
B |
Boolean |
TRue |
|
H |
Hash code |
Listen 28b2 |
|
TX |
Date and Time |
See the time introduction table. |
|
% |
Percent sign |
% |
|
N |
Delimiter |
|
Multiple flags can be used in the printf function, such as System. out. printf ("%,. 2f", 10000.0/3.0 );
The following table lists the flags used for printf.
|
Flag |
Purpose |
Example |
|
+ |
Print the symbol before the number |
+ 3333.33 |
|
Space |
Add a space before a positive number. |
| 1, 3333.33 | |
|
0 |
Add 0 before the number |
003333.33 |
|
- |
Left aligned |
| 1, 3333.33 | |
|
( |
Negative values are enclosed in parentheses. |
(3333.33) |
|
, |
Add group Separator |
3,333.33 |
|
# (For f) |
Contains decimal point |
3,333. |
|
# (For x or o) |
Add prefix 0x or 0 |
0 xcafe |
|
^ |
Convert to uppercase |
0 XCAFE |
|
$ |
Specify the format parameter index. For example, % 1 $ d and % 1 $ d indicate that the index is in decimal format.
And the hexadecimal printing of the first parameter |
159 9F |
|
< |
Format the preceding parameters. For example, % d % <x indicates that the parameters are in decimal and hexadecimal notation.
Print the same Parameter |
159 9F |
The time and date Conversion characters are as follows:
|
Conversion character |
Type |
Example |
|
C |
Complete Date and Time |
Mon Feb 0918:05:19 PST 2004 |
|
F |
ISO 8601 date |
2004-02-09 |
|
D |
Us time format (mm/dd/year) |
02/09/2004 |
|
T |
24 hours |
18:05:19 |
|
R |
12 hours |
06:05:19 |
|
R |
24 hours, no second |
|
|
Y |
Four years |
2004 |
|
Y |
The last two |
04 |
|
C |
The first two |
20 |
|
B |
Complete monthly spelling |
February |
|
B or h |
Abbreviation of month |
Feb |
|
M |
Two months (add 0 before) |
02 |
|
D |
Two days (add 0 before) |
09 |
|
E |
Date (do not add 0 before) |
9 |
|
A |
Full day of week |
Monday |
|
A |
Abbreviation of day of the week |
Mon |
|
J |
The number of days of the year, and the third digit is 0. |
069 |
|
H |
24-hour system, 0 for two digits |
18 |
|
K |
In 24-hour format, two do not add 0 |
18 |
|
I |
12-hour system, 0 for two digits |
06 |
|
L |
The hour in 12-hour format. |
6 |
|
M |
Minute, 0 for two digits |
05 |
|
S |
Second, complement 0 |
19 |
|
L |
Millisecond, supplemented by three Zeros |
047 |
|
N |
In microseconds, nine-bit complement 0 |
047000000 |
|
P |
Upper or lower case |
PM |
|
P |
Lower case in upper afternoon |
Pm |
|
Z |
RFC 822 numeric offset from GMT |
-0800 |
|
Z |
Time Zone |
PST |
|
S |
00:00:00 seconds |
1078884319 |
|
E |
00:00:00 Ms count |
1078884319047 |
2.2 String. format
String formatLeftS(String str, = "%-" + (min_length < 1 ? 1 : min_length) + "s" String format0Right( num, = "%0" + (min_length < 1 ? 1 : min_length) + "d" String format0Right( d, min_length, = "%0" + (min_length < 1 ? 1 : min_length) + "." + (precision < 0 ? 0 : precision) + "f"
2.3 others ......