Differences between sprintf and printf functions in php The following is an example: Rounding the last two decimal places to retain & lt ;? Php $ num121; echosprintf (% 0.2f, $ num1 ). & lt; br & gt; output 21.00 $ num216.3287; echosprintf (% 0.2f, $ num2 ). & lt; br & gt; differences between sprintf and printf functions in php
The following is an example: rounding to the last two decimal places
"; // Output 21.00 $ num2 = 16.3287; echo sprintf (" % 0.2f ", $ num2 )."
"; // Output 16.33 $ num3 = 32.12329; echo sprintf (" % 0.2f ", $ num3 )."
"; // Output 32.12?>
Explain the meaning of % 0.2f:
% Indicates the start character.
0 indicates that the vacancy is filled with 0
2 indicates two digits after the decimal point
F indicates converting to a floating point number.
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.
Differences between printf and sprintf
1. printf function:
Int printf (string format [, mixed args [, mixed...])
Produces output according to format, which is described in the documentation for sprintf ().
Returns the length of the outputted string.
Format the text and output it, for example:
$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 not printed, the formatted text is returned. Others are the same as printf.
3. print function:
Yes. one value can be returned and only one parameter is allowed.
Int print (string arg)
Outputs arg. Returns 1, always.