Detailed usage in the java printf

See this article is well written, can not help but reprint

This article is reprinted from https://www.cnblogs.com/seakt/p/4478045.html

Printf currently supports the following formats: 
  

Copy the code
          single character% c 
          % d decimal integer 
          % f decimal floating 
          % O octal number 
          % s string 
          % u unsigned decimal 
          % x hexadecimal number 
          %%% Output Percent
Copy the code

 


printf full format of the format of the control: 
% - Mn 0 h L format or the character 
of the composition of the format described below will be described: 
①%: indicating a start symbol format described indispensable. 
②-: Yes - represents a left-aligned output, such as a right-aligned output are omitted. 
③0: 0 0 indicates that the specified gap fill, such as fill not assigned slot are omitted. 
④m.n: m refers to the wide-field, i.e., the number of entries corresponding to the character output on the output device occupied. N refers to the accuracy. Real numbers for explaining the output of decimal places. Is designated n, the implicit precision of n = 6 bits. 
⑤l or h: l refers to long integer type, double type of real finger. h for the character format for the short integer type of correction. 
--------------------------------------- 
Format Character 
Format Character designating data for output items type and output formats. 
①d format: for outputting decimal integer. There are several uses: 
  % D: output according to the actual size of the integer data. 
  % md: m output field specified width. If the number of data is smaller than m, to fill the spaces left, if greater than m, at the actual number of bits output. 
  % ld: long data output. 
②o format: unsigned octal integer output. Long can be used for "% lo" output format. The same can also be specified with the field width "% mo" output format. 
example: 

   main() { 
     int a = -1; 
     printf("%d, %o", a, a); 
   } 

 


  Run Results: -1,177777 
  program analysis: 1 in the memory unit (stored in two's complement form) is (1111111111111111) 2, octal number (177 777) 8. 
③x formats: unsigned integer output in hexadecimal form. Long can be used for "% lx" output format. The same can also be specified with the field width "% mx" output format. 
④u formats: unsigned integer decimal output. It may be "% lu" output format for a long integer. The same can also be specified with the field width "% mu" output format. 
⑤c format: output a character. 
⑥s format: a string to output. There are several usage 
  % s: example: printf ( "% s", "CHINA") outputs "CHINA" string (without quotation marks). 
  % ms: m columns representing the output string, the string itself as a length greater than m, m is eligible to break out, the entire output string. If the string length is less than m, then fill the spaces left. 
  % -ms: If the string length is less than m, m is in the range of the column, by the string left, up and right spaces. 
  % m.ns: m columns representing output, but only to take a string of n characters left. This output characters to the right of n and m columns, fill the space left. 
  % -m.ns: wherein m, n are as above, output of n-characters in the left column of the range of m right up space. If n> m, n values are automatically taken, which is to ensure the normal output n characters. 
⑦f format: for outputting a real number (including single, double), the output of a decimal. There are several uses: 
  % F: width is not specified, and outputs the integer part of the total output 6 decimal places. 
  % m.nf: output accounted for m columns, where n has a decimal places, such as the value m is smaller than the width of the left end of the meeting spaces. 
  % -m. 
⑧e formats: Real output exponentially. Available in the following forms: 
  % E: the digital part (known as a mantissa) 6 outputs fractional exponent portion comprises four or five. 
  % m.ne and% -m.ne: m, n, and "-" characters the same meaning as before. Where n denotes a decimal portion of the digital data, m represents the whole width of the output data occupied. 
⑨g format: f format or automatic selection of a shorter e output format, does not output the zero meaningless. 
--------------------------------------- 
Further information on the printf function: 
If you want to output characters "%", two consecutive% should be indicated by a "format control" string, such as: 
the printf ( "% F %%", 1.0 /. 3); 
output 0.333333%. 
--------------------------------------- 
For single precision number, the output symbol using the% f , only the first seven bits of significant digits, six decimal. 
For doubles,% lf using output formatter, the first 16 bits are valid numbers, decimal 6. 
###################################### Supplements ########### ############################# 
by the expert guidance 
for mn format may also be represented by the following methods (e) 
char CH [20 is] ; 
the printf ( "% * S * \ n-.", m, n-, CH); 
front * definition is the total width, the number is defined behind the output. Respectively outside parameters m and n. I think the benefits of this approach is that the parameters m and n can be assigned in addition to the statement, so as to control the output format. 
-------------------------------------------------- ------------------------------ 
today saw% n output format string length value can be assigned output Dai a variable, see the example below: 
int sLen; 
the printf ( "% n-Hello World", & sLen); 
after performing variable is assigned the value 11. 
Do not remember that we look at or helpful, especially scanf formatted input, let me profusely, before its too little to understand. The original regularization can also be used inside. 
In reading the source code of their own time, also we found a number of so-called above-mentioned. Slowly accumulated, and look for their own readers to enjoy. 
"UNIX System Programming" P9 (in English P15): 
fprintf (stderr, "% P and AT A \ NX AT P% \ n-", (void *) A, (viod *) & X); 
wherein the mentioned% p, I understand format printing pointer address. 
-------------------------------------------------- -------------------------- 

Copy the code
{class TestPrintf public 
public static void main (String [] args) { 
// definition of variables, for formatting output. 
345.678 D = Double; 
String S = "Hello!"; 
int I = 1234; 
// "%" indicates the output format, after the contents of "%" in a defined format. 
System.out.printf ( "% f", d ); // "f" represents a floating point formatted output. 
System.out.println (); 
System.out.printf ( "% 9.2f", D); //"9.2 length "represents the output 9 after 2 represents the number of decimal places. 
System.out.println (); 
System.out.printf ( "% 9.2f +", D); // "+" represents a signed output. 
System.out.println (); 
System.out.printf ( "% - 9.4f", D); // "-" represents the output of the left alignment (default is right-aligned). 
System.out.println (); 
System.out.printf ( "% + - 9.3f", D); // "+ -" represents the output of the left-justified number and a signed number. 
System.
System.out.println (); 
System.out.printf ( "% O", I); // "O" represents an output octal integer. 
System.out.println (); 
System.out.printf ( "X%", I); // "D" represents a hexadecimal integer output. 
System.out.println (); 
System.out.printf ( "% X #", I); // "D" represents a hexadecimal integer with the output flag. 
System.out.println (); 
System.out.printf ( "% S", S); // "D" represents the output string. 
System.out.println (); 
System.out.printf ( "a floating point output:% f, an integer:% d, a string:% S", D, I, S); 
// may output a plurality of variable, pay attention to the order. 
System.out.println (); 
System.out.printf ( "String:% 2 $ s,% 1 $ d hexadecimal number: # $%. 1 X", I, S); 
// "X- $ "represents the first of several variables. 
} 
}
Copy the code

Printf currently supports the following formats: 
  

Copy the code
          single character% c 
          % d decimal integer 
          % f decimal floating 
          % O octal number 
          % s string 
          % u unsigned decimal 
          % x hexadecimal number 
          %%% Output Percent
Copy the code

 


printf full format of the format of the control: 
% - Mn 0 h L format or the character 
of the composition of the format described below will be described: 
①%: indicating a start symbol format described indispensable. 
②-: Yes - represents a left-aligned output, such as a right-aligned output are omitted. 
③0: 0 0 indicates that the specified gap fill, such as fill not assigned slot are omitted. 
④m.n: m refers to the wide-field, i.e., the number of entries corresponding to the character output on the output device occupied. N refers to the accuracy. Real numbers for explaining the output of decimal places. Is designated n, the implicit precision of n = 6 bits. 
⑤l or h: l refers to long integer type, double type of real finger. h for the character format for the short integer type of correction. 
--------------------------------------- 
Format Character 
Format Character designating data for output items type and output formats. 
①d format: for outputting decimal integer. There are several uses: 
  % D: output according to the actual size of the integer data. 
  % md: m output field specified width. If the number of data is smaller than m, to fill the spaces left, if greater than m, at the actual number of bits output. 
  % ld: long data output. 
②o format: unsigned octal integer output. Long can be used for "% lo" output format. The same can also be specified with the field width "% mo" output format. 
example: 

   main() { 
     int a = -1; 
     printf("%d, %o", a, a); 
   } 

 


  Run Results: -1,177777 
  program analysis: 1 in the memory unit (stored in two's complement form) is (1111111111111111) 2, octal number (177 777) 8. 
③x formats: unsigned integer output in hexadecimal form. Long can be used for "% lx" output format. The same can also be specified with the field width "% mx" output format. 
④u formats: unsigned integer decimal output. It may be "% lu" output format for a long integer. The same can also be specified with the field width "% mu" output format. 
⑤c format: output a character. 
⑥s format: a string to output. There are several usage 
  % s: example: printf ( "% s", "CHINA") outputs "CHINA" string (without quotation marks). 
  % ms: m columns representing the output string, the string itself as a length greater than m, m is eligible to break out, the entire output string. If the string length is less than m, then fill the spaces left. 
  % -ms: If the string length is less than m, m is in the range of the column, by the string left, up and right spaces. 
  % m.ns: m columns representing output, but only to take a string of n characters left. This output characters to the right of n and m columns, fill the space left. 
  % -m.ns: wherein m, n are as above, output of n-characters in the left column of the range of m right up space. If n> m, n values are automatically taken, which is to ensure the normal output n characters. 
⑦f format: for outputting a real number (including single, double), the output of a decimal. There are several uses: 
  % F: width is not specified, and outputs the integer part of the total output 6 decimal places. 
  % m.nf: output accounted for m columns, where n has a decimal places, such as the value m is smaller than the width of the left end of the meeting spaces. 
  % -m. 
⑧e formats: Real output exponentially. Available in the following forms: 
  % E: the digital part (known as a mantissa) 6 outputs fractional exponent portion comprises four or five. 
  % m.ne and% -m.ne: m, n, and "-" characters the same meaning as before. Where n denotes a decimal portion of the digital data, m represents the whole width of the output data occupied. 
⑨g format: f format or automatic selection of a shorter e output format, does not output the zero meaningless. 
--------------------------------------- 
Further information on the printf function: 
If you want to output characters "%", two consecutive% should be indicated by a "format control" string, such as: 
the printf ( "% F %%", 1.0 /. 3); 
output 0.333333%. 
--------------------------------------- 
For single precision number, the output symbol using the% f , only the first seven bits of significant digits, six decimal. 
For doubles,% lf using output formatter, the first 16 bits are valid numbers, decimal 6. 
###################################### Supplements ########### ############################# 
by the expert guidance 
for mn format may also be represented by the following methods (e) 
char CH [20 is] ; 
the printf ( "% * S * \ n-.", m, n-, CH); 
front * definition is the total width, the number is defined behind the output. Respectively outside parameters m and n. I think the benefits of this approach is that the parameters m and n can be assigned in addition to the statement, so as to control the output format. 
-------------------------------------------------- ------------------------------ 
today saw% n output format string length value can be assigned output Dai a variable, see the example below: 
int sLen; 
the printf ( "% n-Hello World", & sLen); 
after performing variable is assigned the value 11. 
Do not remember that we look at or helpful, especially scanf formatted input, let me profusely, before its too little to understand. The original regularization can also be used inside. 
In reading the source code of their own time, also we found a number of so-called above-mentioned. Slowly accumulated, and look for their own readers to enjoy. 
"UNIX System Programming" P9 (in English P15): 
fprintf (stderr, "% P and AT A \ NX AT P% \ n-", (void *) A, (viod *) & X); 
wherein the mentioned% p, I understand format printing pointer address. 
-------------------------------------------------- -------------------------- 

Copy the code
{class TestPrintf public 
public static void main (String [] args) { 
// definition of variables, for formatting output. 
345.678 D = Double; 
String S = "Hello!"; 
int I = 1234; 
// "%" indicates the output format, after the contents of "%" in a defined format. 
System.out.printf ( "% f", d ); // "f" represents a floating point formatted output. 
System.out.println (); 
System.out.printf ( "% 9.2f", D); //"9.2 length "represents the output 9 after 2 represents the number of decimal places. 
System.out.println (); 
System.out.printf ( "% 9.2f +", D); // "+" represents a signed output. 
System.out.println (); 
System.out.printf ( "% - 9.4f", D); // "-" represents the output of the left alignment (default is right-aligned). 
System.out.println (); 
System.out.printf ( "% + - 9.3f", D); // "+ -" represents the output of the left-justified number and a signed number. 
System.
System.out.println (); 
System.out.printf ( "% o", i ); // "o" represents an output octal integer. 
System.out.println (); 
System.out.printf ( "X%", I); // "D" represents a hexadecimal integer output. 
System.out.println (); 
System.out.printf ( "% X #", I); // "D" represents a hexadecimal integer with the output flag. 
System.out.println (); 
System.out.printf ( "% S", S); // "D" represents the output string. 
System.out.println (); 
System.out.printf ( "a floating point output:% f, an integer:% d, a string:% S", D, I, S); 
// may output a plurality of variable, pay attention to the order. 
System.out.println (); 
System.out.printf ( "String:% 2 $ s,% 1 $ d hexadecimal number: # $%. 1 X", I, S); 
// "X- $ "represents the first of several variables. 
} 
}
Copy the code

Guess you like

Origin www.cnblogs.com/niliuxiaocheng/p/11899697.html