Here's an example: rounding to keep two decimal places
<?PHP$NUM1 = 21;echo sprintf ("%0.2f", $num 1). " <br/> "; Output 21.00$num2 = 16.3287;echo sprintf ("%0.2f", $num 2). " <br/> "; Output 16.33$num3 = 32.12329;echo sprintf ("%0.2f", $num 3). " <br/> "; 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:
$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.
Differences between sprintf and printf function usage in PHP