Let's take a look at the following instructions:
Modifier format description meaning
Int A = 1; int B = 1234; double C = 1.2345678; printf ("% 2D \ n", a); printf ("% + 2d \ n", ); printf ("% 4D \ n", a); printf ("% 2D \ n", B); printf ("% + 2d \ n", B ); printf ("% 4D \ n", B); printf ("% 2.2f \ n", C );
M % MD outputs the integer value in width m. If the value is less than m, fill in spaces on the left.
0 m % 0md outputs the integer number with the width m. If the value is less than m, the value is left zero.
M, N % m. NF output real decimal places in width m, and decimal places are n digits
Double I = 12.3,
Printf ("% 2f", I, );
Output: 12.300000
Printf ("% 2.1f", I, );
Output: 12.3
When the specified field width is less than the actual width of the data, the number of integers is output according to the actual field width of the number, and the number of decimal places is rounded to the floating point number. Int I = 1; printf ("% 04d", I );
Output: 0001% 04d, 4 digits in decimal format. If it is not enough, add 0 in front.