Deep analysis of the printf () function format using _php instance in PHP

Source: Internet
Author: User

The advantage of the printf () function is that you can format the output!

Full format control for printf:

%-0 M.N L or H format characters

The following is a description of the constituent format description:

①%: Represents the starting symbol for the format description and is not missing.

②-: Has-represents the left-aligned output, such as omitting the right alignment output.

③0:0 indicates that the specified vacancy is filled with 0, such as omitting the specified vacancy.

④m.n:m refers to the field width, that is, the number of characters that the corresponding output item occupies on the output device. n refers to precision. The number of decimal places to use to describe the actual number of outputs. When n is specified, the implied precision is the n=6 bit.

⑤l or h:l the integer to the long type, and to the real type double. H is used to Fu Xiu the format character of an integral type to the short type.

---------------------------------------
Format Characters

The format character is used to specify the data type and output format of the output item.

①d format: Used to output decimal integers. There are several uses for this:

%d: output by the actual length of the integer data.

%md:m is the width of the specified output field. If the number of digits in the data is less than M, then the left is filled with spaces, and if greater than M, the actual digits are output.

%LD: Output Long integer data.

②o format: Output integer in unsigned octal form. The long integer can be output in "%lo" format. You can also specify that the field width is output in "%mo" format.

Cases:

Main () 
{int a =-1; 
printf ("%d,%o", A, a); 

Run Result: -1,177777

Program parsing:-1 in the memory unit (in the complement form) for (1111111111111111) 2, converted to octal number is (177777) 8.

③x format: Output integer in unsigned hexadecimal form. The long integer can be output in "%LX" format. You can also specify that the field width is output in "%MX" format.

④u format: integer output in unsigned decimal form. The long integer can be output in "%lu" format. You can also specify that the field width is output in "%MU" format.

⑤c format: output one character.

⑥s format: Used to output a string. There are several ways to use it.

%s: For example: printf ("%s", "a") exports the "" "string (excluding double quotes).

%ms: The output string takes up m columns, such as the string itself is greater than m, then breaks the limit of M and outputs all the strings. If the string length is less than m, the left space is filled.

%-ms: If the string length is less than M, then in the M column range, the string is left, and the right space is filled.

%M.NS: Output takes the M column, but takes only n characters from the left end of the string. The n characters are printed on the right side of the M column, and the left space is filled.

%-M.NS: Where the meaning of M, n above, n characters output in the range of M column to the left, right complement space. If n>m, the N value is automatically taken, that is, the normal output of n characters is guaranteed.

⑦F format: Used to output real numbers (including single, double precision), output in decimal form. There are several uses for this:

%f: Does not specify the width, the integer part outputs all and outputs 6 decimal places.

%M.NF: Output is a total of M column, which has n decimal places, such as the value width is less than m left fill space.

%-M.NF: Output A total of n columns, where there are n decimal places, such as the value width is less than m right side complement space.

⑧e format: outputting real numbers in exponential form. The following forms are available:

%e: The digital portion (also called the mantissa) outputs 6 decimal places, and the exponent portion occupies 5 or 4 digits.

The%m.ne and%-m.ne:m, N, and "-" characters have the same meaning as before. Here n refers to the number of decimal places in the numeric portion of the data, and m represents the width of the total output data.

⑨g format: Automatically select a shorter output in the F format or e format, and do not output meaningless 0.

---------------------------------------
Further instructions on the printf function:

If you want to output the character "%", you should use two consecutive% in the format control string, such as:

printf ("%f%%", 1.0/3);

Output 0.333333%.

---------------------------------------
For single-precision numbers, the first 7 digits are valid digits, with a decimal 6 digits, when using the%f format character output.

For double-precision numbers, the first 16 digits are valid digits, with a decimal number of 6 digits, when using the%LF format character output.

The above in-depth analysis of PHP in the printf () function format is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.