Java C format input using printf function

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.