Java System. Out. printf () Usage

Source: Internet
Author: User
Tags format definition
Package test; public class main {public static void main (string [] ARGs) {// defines some variables for formatting the output. Double D = 345.678; string S = "Hello! "; Int I = 1234; //" % "indicates formatted output. The content after" % "is the format definition. System. Out. printf ("% F", d); // "F" indicates formatting the output floating point number. System. out. println (); 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. println (); system. Out. printf ("% + 9.2f", d); // "+" indicates that the output number carries a positive or negative number. System. Out. println (); system. Out. printf ("%-9.4f", d); // "-" indicates the left alignment of the number of outputs (right alignment by default ). System. out. println (); system. out. printf ("% +-9.3f", d); // "+-" indicates that the number of outputs carries positive and negative signs and is left aligned. System. Out. println (); system. Out. printf ("% d", I); // "D" indicates the output of a decimal integer. System. Out. println (); system. Out. printf ("% O", I); // "O" indicates the output of an octal integer. System. Out. println (); system. Out. printf ("% x", I); // "D" indicates the hexadecimal integer. System. Out. println (); system. Out. printf ("% # X", I); // "D" indicates the integer with the hexadecimal mark. System. Out. println (); system. Out. printf ("% s", S); // "D" indicates the output string. System. out. println (); system. out. printf ("output a floating point: % F, an integer: % d, a string: % s", D, I, S); // multiple variables can be output, pay attention to the sequence. System. out. println (); system. out. printf ("string: % 2 $ S, % 1 $ D hexadecimal number: % 1 $ # X", I, S ); // "x $" indicates the number of variables .}}

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.