Php printf function formatting (1/2)

Source: Internet
Author: User

Syntax
Printf (format, arg1, arg2, arg ++) parameter description
Format is required. Specifies the string and how to format the variable.
Arg1 is required. Specifies the parameter inserted at the first % symbol in the formatted string.
Optional. Specifies the parameter inserted at the second % symbol in the formatted string.
Optional. Specifies the parameters inserted in the third, fourth, and other % symbols of the formatted string.

Description
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.
Tips and comments
Note: If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "$. See Example 3.

The complete format of printf format control:
%-0 m. n l or h characters
The following describes the composition formats:
① %: Indicates the starting symbol of the format description, which is indispensable.
②-: "Yes" indicates the left-aligned output. If it is omitted, "Yes" indicates the right-aligned output.
③ 0: if there is 0, it indicates that the specified vacant space is filled with 0. If it is omitted, it indicates that the specified vacant space is not filled.
④ M. n: m indicates the domain width, that is, the number of characters that the corresponding output item occupies on the output device. N indicates the precision. It is used to describe the decimal places of the output real number. If n is specified, the implicit precision is n = 6 digits.
⑤ L or h: l refers to long type for integer type and double type for real type. H is used to modify the integer format characters 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 a decimal integer. It has the following usage:
% D: output according to the actual length of integer data.
% Md: m is the width of the specified output field. If the number of digits of the data is smaller than m, spaces are filled on the left side. If the number is greater than m, the data is output based on the actual number of digits.
% Ld: Output long integer data.
② O format: outputs integers in the unsigned octal form. You can output long integers in "% lo" format. You can also specify the field width to output in "% mo" format.
Example:
Main ()
{Int a =-1;
Printf ("% d, % o", a, );
}
Running result:-1, 177777
Program parsing: the value of-1 is (1111111111111111) 2 in the inner storage unit (stored in the form of a supplemental code), and the value of-1 is (177777) 8.
③ X format: returns an integer in the unsigned hexadecimal format. You can output long integers in "% lx" format. You can also specify the field width to output in "% mx" format.
④ U format: returns an integer in unsigned decimal format. You can output long integers in "% lu" format. You can also specify the field width to output in "% mu" format.

Homepage 1 2 Last page

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.