[Original walkerlee] study notes on the sprintf () function of PHP _ PHP Tutorial

Source: Internet
Author: User
[Original walkerlee] notes on learning PHP function sprintf. P study notes on PHP function sprintf () sprintf formatting strings. Syntax: stringsprintf (stringformat, mixed [args]...); return value: string type: data processing P> Study Notes on the sprintf () function of PHP
Sprintf
Format the string.
Syntax: string sprintf (string format, mixed [args]...);
Return value: string
Correspondence type: data processing

Description
This function is used to format the string. The format parameter is the conversion format, starting with the percent sign % to the conversion character. The format of the conversion includes
1. fill in the blanks. If the value is 0, it indicates that the space is filled with 0; if the space is an internal value, it indicates that the space is placed.
2. alignment. The value is aligned to the right, and the negative number table is aligned to the left.
3. column width. Minimum width.
4. accuracy. The number of digits after the decimal point.
Type. See the following table.
=-
Conversion character
=-
% Indicates the percentage, which is not converted.
The integer B is converted into binary.
Convert an integer in c to an ASCII character.
D integer to decimal place.
F times the precision number to the floating point number.
O integer to octal.
The s integer is converted into a string.
Convert an integer to a lowercase hexadecimal value.
Convert X to uppercase hexadecimal.
=-
=- =-
Example
=- =-
Example
$ Money1 = 68.75;
$ Money2 = 54.35;
$ Money = $ money1 + $ money2;
// The variable $ money value is "123.1 ";
$ Formatted = sprintf ("% 01.2f", $ money );
// The variable $ formatted value is "123.10"
?>
=- =-
What does % 01.2f mean?
The "%" symbol indicates the beginning. it is written at the beginning to indicate that the specified format is about to begin.
That is, "Start character", until "conversion character" appears, even if the format ends.
The value 0 following the "%" symbol indicates that the zero character is a "fill-in character". if the position is empty, fill it with 0.
The value 1 after 0 indicates that the number before the decimal point must have more than one place.
If you change 1 to 2, if the value of $ money is 1.23, the value of $ formatted is 01.23.
Because the number before the decimal point occupies only one digit, according to the above format, the number before the decimal point should occupy two digits, and now only one digit, so fill with 0.
Till now,. 2 () is easy to understand. it means that the number after the decimal point must take two places. if $ money is set to 1.234 at this time, $ formatted is set to 1.23.

Sprintf () study notes sprintf will format strings. Syntax: string sprintf (string format, mixed [args]...); return value: string type: data processing...

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.