When using system. Out. Print and system. Out. println, we often suffer from writing complicated Code And even use textformat and other format output classes. In fact, these are not required. Java has its own formatting input method, which is comparable to C format input. It is the system. Out. printf function.
For example, we use
System. Out. printf ("print from % 3d to % 3d integer \ n", 1,100 );
The output result after running is:
Print an integer from 1 to 100
The % 3d above indicates the input integer, which must be at least three digits.
System. Out. printf can be used in the following ways:
System. Out. printf ("% F", d); // "F" indicates formatting the output floating point number.
System. Out. printf ("% 9.2f", d); // 9 in "9.2" indicates the length of the output, and 2 indicates the number of digits after the decimal point.
System. Out. printf ("% + 9.2f", d); // "+" indicates that the output number carries a plus or minus sign.
System. Out. printf ("%-9.4f", d); // "-" indicates the left alignment of the number of outputs (right alignment by default ).
System. Out. printf ("% +-9.3f", d); // "+-" indicates that the output number carries a plus or minus sign and is left aligned.
System. Out. printf ("% d", I); // "D" indicates the output decimal integer.
System. Out. printf ("% 6D", I); // indicates the input of a decimal integer with at least 6 digits.
System. Out. printf ("% O", I); // "O" indicates the output of an octal integer.
System. Out. printf ("% x", I); // "X" indicates the hexadecimal integer.
System. Out. printf ("% # X", I); // "X" indicates the integer with the hexadecimal mark.
System. Out. printf ("% s", S); // "S" indicates the output string.
ArticleAddress: http://javapub.iteye.com/blog/719865