In-depth analysis of the printf () function formatting used in PHP, in-depth anatomy of the printf_php tutorial

Source: Internet
Author: User

In-depth analysis of the printf () function formatting used in PHP, in-depth analysis of printf


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

The full format of printf format control:

%-0 M.N L or H format characters

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.

---------------------------------------
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 ways to use this:

%d: Output based on the actual length of the integer data.

%md:m the width of the specified output field. If the number of bits in the data is less than M, then the left is a space, and if it is greater than M, the actual number of digits is output.

%LD: Outputs long-integer data.

②o format: outputs integers in unsigned octal form. The long integer type can be output in the format "%lo". You can also specify the field width to be output in "%MO" format.

Cases:

Run Result: -1,177777

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

③X format: outputs integers in unsigned hexadecimal form. The long integer type can be output in the format "%LX". You can also specify the field width to be output in "%MX" format.

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

⑤C format: outputs one character.

⑥s format: Used to output a string. There are several uses.

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

%ms: The output of the string in the M-column, such as the string itself is greater than M, then the limit of M, the string is all output. If the string length is less than M, the left fill space.

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

%M.NS: The output occupies M-column, but only the left-hand side of the string is n characters. The n characters output to the right of the M column and left to fill the blanks.

%-m.ns: where m and n mean the same, n characters output to the left of the M-column range and right-fill the 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 and double precision), output in decimal form. There are several ways to use this:

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

%M.NF: The output is a total of M columns, where there are n decimal places, such as the value width is less than the left fill space.

%-M.NF: The output is a total of n columns, where there are n decimal places, such as the value width is less than m right end fill space.

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

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

%m.ne and%-m.ne:m, N, and "-" characters have the same meaning as before. Here n refers to the number of decimal digits of the data, and m represents the width of the entire output data.

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

---------------------------------------
A further explanation of the printf function:

If you want to output the character "%", you should indicate a continuous two% in the format control string, such as:

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

Output 0.333333%.

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

For double-precision numbers, when using the%LF format character output, the first 16 digits are valid digits, and the decimal 6 bits.

In this article in-depth analysis of the printf () function format in PHP is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support a lot of help the home.

http://www.bkjia.com/PHPjc/1133102.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133102.html techarticle in-depth analysis of the printf () function formatting used in PHP, the advantage of the printf printf () function is that you can format the output! printf Format Control full format:%-0 M.N L or h format word ...

  • 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.