Notes on learning sprintf () function in PHP

Source: Internet
Author: User

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.
Why is 4 missing? Because, according to the preceding rules after the decimal point, only two digits are required. However, in the $ money value, the decimal point occupies three places. Therefore, 4 is removed and only 23 is left.

Finally, it ends with f "conversion character". For other Conversion characters, refer to the conversion character list above.

About alignment

If a minus sign is added to the start symbol of %, the number is aligned to the right.

Column

<?
$ Money = 1.4;
$ Formatted = sprintf ("%-02.2f", $ money );
Echo $ formatted;
?>

At this time, $ formatted will not be 01.40 but 1.400

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.