sprintf () function in PHP study Note _php Tutorial

Source: Internet
Author: User

sprintf

Formats the string.

Syntax: string sprintf (string format, mixed [args] ...);

return value: String

Function type: Data processing

Content Description

This function is used to format a string. The parameter format is the converted form, starting with the percent sign% and ending with the conversion character. The converted format consists of the

1. Fill in the blanks. 0 words means blank 0, space is the default value, indicating that the space is placed.

2. Alignment. The default value is aligned to the right and the minus table is aligned to the left.

3. Field width. To the minimum width.

4. Accuracy. Refers to the number of floating-point digits after the decimal point.

Type, see table below

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Convert characters
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% printed percent symbol, not converted.
b integer turns into binary.
The c integer is converted to the corresponding ASCII character.
D integers are turned into 10.
The F-Times precision number is converted to floating point numbers.
o integers are turned into octal.
The s integer is converted into a string.
The x integer is converted to lowercase 16 rounding.
X integers are converted to uppercase 16 rounding.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Example

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Usage examples

$money 1 = 68.75;
$money 2 = 54.35;
$money = $money 1 + $money 2;
At this time the variable $money value is "123.1";
$formatted = sprintf ("%01.2f", $money);
At this time the variable $ formatted value is "123.10"
?>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

What does this%01.2f mean?

First this% symbol is the beginning of the meaning, he writes at the top to indicate that the specified format is to begin. That is, the "starting character" until the "convert character" appears, even if the format is terminated.

Then followed by the% symbol is 0 This 0 is the "fill in the blanks" means that if the location is empty, fill with the total.
After 0 is 1 of this 1 is the rule, the number before the decimal point to occupy more than 1 digits.

If you change 1 to 2 if the value of the $money is 1.23, then the value of the $formatted will be 01.23
Because the number in front of the decimal point only accounted for 1 bits, according to the format specified above, the number should be 2 digits before the decimal point, and now only 1, so, with a total of three.

At present, behind the%01 of the. 2 (point 2) is very well understood, it means, the rule, the number after the decimal point, must occupy 2 bits. If this is the $money value is 1.234, the $formatted value will be 1.23.
Why is the 4 gone? Because, after the decimal point in accordance with the above provisions, must and only 2 bits can be accounted for. However $money value, the decimal point accounted for 3 bits, so 4 was removed, leaving only 23.

Finally, at the end of the F "convert character", please refer to the conversion character list above for other conversion characters.

About alignment

If you add a-(minus sign) after the% start symbol, the numbers will be treated in the right-aligned manner.

Liezi

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

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

http://www.bkjia.com/PHPjc/508242.html www.bkjia.com true http://www.bkjia.com/PHPjc/508242.html techarticle sprintf to format the string. Syntax: string sprintf (string format, mixed [args] ...); Return value: String function type: Data processing content Description This function is used to format a string. ...

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