sprintf(string format, mixed [args]...);
Retourne une chaîne formatée avec le format format.
La chaîne de formet est composée de 0 ou plus directives : généralement des caractères qui sont recopiés tels quel (hormis %), et des spécifications, chacune delle disposant de son propre paramètre. Cela sapplique à sprintf() et printf()
Chaque conversion consistent dans lordre :
% - a literal percent character. No argument is required. |
b - the argument is treated as an integer, and presented as a binary number. |
c - the argument is treated as an integer, and presented as the character with that ASCII value. |
d - the argument is treated as an integer, and presented as a decimal number. |
f - the argument is treated as a double, and presented as a floating-point number. |
o - the argument is treated as an integer, and presented as an octal number. |
s - the argument is treated as and presented as a string. |
x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). |
X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). |
See also: printf(), number_format()
Example 1. sprintf: zero-padded integers
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); |
Example 2. sprintf: formatting currency
$money1 = 68.75; $money2 = 54.35; $money = $money1 + $money2; // echo $money will output "123.1"; $formatted = sprintf ("%01.2f", $money); // echo $formatted will output "123.10" |