sprintf
Formats the string.
Syntax: string sprintf (string format, mixed [args] ...);
return value: String
Types of functions: Data processing
Content Description
This function is used to format strings. The parameter format is the format of the transformation, starting with the percent sign to the converted character. The format of the conversion is included in the
1. Fill in the blanks. 0 words to fill in 0 space, space is the default value, indicating that the space is placed.
2. Alignment method. The default value is aligned to the right, and the minus table is aligned to the left.
3. Field width. is the minimum width.
4. Accuracy. Refers to the number of floating-point digits after the decimal point.
Type, see the table below
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Convert character
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% print percent symbol, do not convert.
The b integer is converted into binary.
The c integer is converted to the corresponding ASCII character.
The d integer is turned into 10.
The F times the precision number is converted to floating point numbers.
The o integer is converted into octal.
The s integer turns into a string.
The x integer is converted to lowercase 16.
The X integer is converted to uppercase 16.
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 of all this% symbol is the meaning of the beginning, he writes at the front of the specified format to begin. This 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 "fill in the blanks" means that if the location is empty, fill with 0来.
After 0 is 1 This 1 is the rule, the decimal point before the number occupies more than 1 digits.
If you change 1 to 2, if the value of $money is 1.23, the value of the $formatted will be 01.23
Because, the number in front of the decimal point only accounted for 1 digits, according to the format specified above, the decimal point before the number should occupy 2, now only 1, so, fill with 0来.
By now, the. 2 (point 2) behind the%01 is well understood, meaning that the number of decimal digits must be set to 2. If this time, the value of the $money is 1.234, the value of the $formatted will be 1.23.
Why did the 4 disappear? Because, after the decimal point according to the above rules, required and can only occupy 2 digits. But $money value, the decimal point accounted for 3 digits, so, 4 was removed, only 23 left.
Finally, at the end of the F "convert character", the other converted characters refer to the list of converted characters above.
About aligning
If you add a-(minus sign) after the% start symbol, the numbers are processed in the right alignment.
Liezi
?
$money = 1.4;
$formatted = sprintf ("%-02.2f", $money);
Echo $formatted;
?>
At this point, the $formatted will not be 01.40 but 1.400.