PHP/Development/sprintf — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 07:03, 26 мая 2010

sprintf %0.2f

<?
$tax = 1.06;
$subtotal = 10.94;
$total = $tax + $subtotal;
$fmt_total = sprintf ("%0.2f", $total);
echo "$fmt_total";
?>



sprintf: Considers the string an integer and formats it as a decimal number

<?
$total = 110;
$fmt_total = sprintf ("%d", $total);
echo "$fmt_total";
?>



sprintf: Considers the string an integer and formats it with that ASCII value

<?
$total = 110;
$fmt_total = sprintf ("%c", $total);
echo "$fmt_total";
?>



sprintf: Regards the string an integer and formats it as a binary number

<?
$total = 10.94;
$fmt_total = sprintf ("%b", $total);
echo "$fmt_total";
?>



Type Specifiers for printf() and sprintf()

Specifier           Input type                   Output Format
b                   Integer                      Binary number
C                   Integer                      Single character
D                   Integer                      Signed decimal number
U                   Integer                      Unsigned decimal number
F                   Floating-point number        Floating-point number
O                   Integer                      Octal number
S                   String                       String
X                   Integer                      Hexadecimal number with lowercase letters
X                   Integer                      Hexadecimal number with uppercase letters