currently printf supports the following formats:
%c Single character
%d decimal integers
%f Decimal Floating-point number
%o Octal number
%s String
%u unsigned decimal number
%x hexadecimal number
% output percent%
--------------------------------------------------------------------------------------------------------------- -----------
The following describes the items that comprise the format description:
①%: Represents the starting symbol for the format description, which is indispensable.
②-: Yes-Indicates left-aligned output, such as omitting to indicate right-aligned output.
③0:0 indicates that the specified vacancy is filled in 0, as omitted to indicate that the specified space is not filled.
④m.n:m refers to the width of the field, which is the number of characters that the corresponding output item occupies on the output device. n refers to precision. The number of decimal digits used to describe the actual number of outputs. When n is specified, the implied precision is n=6 bit.
⑤l or h:l refers to the integer type long, which refers to the double type of the real type. H is used to Fu Xiu the format character of an integral type to a short type.
1 Packagecom.hone.test;2 3 //"%" means the formatted output, and the content after "%" is defined as the format. 4 Public classPrintf {5 Public Static voidMain (string[] args) {6 DoubleD = 345.678;7String s = "Hello World";8 intA = 123;9 Ten //output of floating-point type OneSystem.out.printf ("%,d", a);//format output data, plus one per three data, A System.out.println (); -System.out.printf ("%f", D);//345.678000 - System.out.println (); the //9 in "9.2" indicates the length of the output, and 2 indicates the number of digits after the decimal point. 345.678 digits not in front of the grid . -System.out.printf ("%9.3f", D); - System.out.println (); -System.out.printf ("%+9.3f", D);//"+" indicates the number of positive and negative outputs + System.out.println (); -System.out.printf ("%-9.3f", D);//"-" indicates the left-justified number of outputs (default is right-justified) + System.out.println (); ASystem.out.printf ("%+-9.3f", D);//"+-" indicates the number of positive and left aligned outputs at System.out.println (); - - //output of integral type -System.out.printf ("%d", a);//"D" indicates an output decimal integer. - System.out.println (); -System.out.printf ("%04d", a);//4 bits, not enough left to add 0 in System.out.println (); -System.out.printf ("%d%%", a);//If you want to output a percentage, you can use a continuous two to System.out.println (); +System.out.printf ("%o", a);//"O" indicates the output octal integer. - System.out.println (); theSystem.out.printf ("%x", a);//"x" indicates the output octal integer. * $ Panax Notoginseng //the output of the string - System.out.println (); theSystem.out.printf ("%s%n", s);//the output string, where%n represents the line break + ASystem.out.printf ("Output a floating-point number:%.3f, an integer:%d, a string:%s", d,a,s);//Output Multiple variables the + } - $}
Usage of printf in Java