Here's an example: rounding to keep two decimal places
Copy the Code code as follows:
$num 1 = 21;
Echo sprintf ("%0.2f", $num 1). "
"; Output 21.00
$num 2 = 16.3287;
Echo sprintf ("%0.2f", $num 2). "
"; Output 16.33
$num 3 = 32.12329;
Echo sprintf ("%0.2f", $num 3). "
"; Output 32.12
?>
Explain the meaning of the following%0.2f:
% indicates the starting character
0 means the vacancy is filled with 0
2 indicates that the decimal point must occupy two digits
f = Convert to floating point number
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.
The difference between printf and sprintf
1. printf function:
int printf (string format [, mixed args [, mixed ...])
Produces output according to format, which are described in the documentation for SPRINTF ().
Returns the length of the outputted string.
The text is formatted for later output, such as:
Copy the Code code as follows:
$name = "Hunte";
$age = 25;
printf ("My name is%s, age%d", $name, $age);
2. sprintf function:
String sprintf (string format [, mixed args [, mixed ...])
Returns a string produced according to the formatting string format.
Similar to printf, but does not print, but returns formatted text, the rest is the same as printf.
3. Print function:
is a function that returns a value that can have only one parameter.
int print (string arg)
Outputs Arg. Returns 1, always.
http://www.bkjia.com/PHPjc/825226.html www.bkjia.com true http://www.bkjia.com/PHPjc/825226.html techarticle Here's an example: Rounding the reserved decimal point after two-bit copy code code is as follows:? php $num 1 = +, echo sprintf ("%0.2f", $num 1). " BR/"; Output 21.00 $num 2 = 16.3287; echo SP ...