PHP/Development/Printf — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
- 1 printf: %05f
- 2 printf: %b
- 3 printf: %d
- 4 printf: %d, %c
- 5 printf: %f
- 6 printf %f,%10f,%-010f,%2.2f
- 7 printf: %f, %.2f
- 8 printf: hexadecimal representation %x
- 9 printf: left-pad a number and specify precision: %05.2f
- 10 printf: Left-pad the string %s, %-6s
- 11 printf: %o
- 12 printf: octal representation %o
- 13 printf: right-pad with dashes: %"--6s
- 14 printf: %s
- 15 printf: Specifying a Field Width
- 16 printf: %x (lowercase x)
- 17 printf: %X (uppercase X)
- 18 Type Specifiers
- 19 Using printf() to Format a List of Product Prices
printf: %05f
<?php
printf ("left-pad numbers with zeroes: %05f\n", 33.22);
?>
printf: %b
<html>
<head>
<title>printf: %b</title>
</head>
<body>
<?php
$number = 543;
printf("Binary: %b<br>", $number );
?>
</body>
</html>
printf: %d
<html>
<head>
<title>printf: %d</title>
</head>
<body>
<?php
$number = 543;
printf("Decimal: %d<br>", $number );
?>
</body>
</html>
printf: %d, %c
<?php
printf ("The character value of %d is %c\n", 72, 72);
?>
printf: %f
<html>
<head>
<title>printf: %f</title>
</head>
<body>
<?php
$number = 543;
printf("Double: %f<br>", $number );
?>
</body>
</html>
printf %f,%10f,%-010f,%2.2f
:
<pre>
<?php
$value = 3.14159;
printf("%f,%10f,%-010f,%2.2f\n", $value, $value, $value, $value);
?>
</pre>
printf: %f, %.2f
<?php
printf ("Control the number of decimals in %f with %.2f\n", 5.1234, 5.1234);
?>
printf: hexadecimal representation %x
<?php
printf ("The hexadecimal representation of %d is %x\n", 92, 92);
?>
printf: left-pad a number and specify precision: %05.2f
<?php
printf ("left-pad a number and specify precision: %05.2f\n", 33.22);
?>
printf: Left-pad the string %s, %-6s
<?php
printf ("Left-pad the string %s with dashes: %"-6s\n", "foo", "foo");
?>
printf: %o
<html>
<head>
<title>printf: %o</title>
</head>
<body>
<?php
$number = 543;
printf("Octal: %o<br>", $number );
?>
</body>
</html>
printf: octal representation %o
<?php
printf ("octal representation is %o)\n", 92);
?>
printf: right-pad with dashes: %"--6s
<?php
printf ("right-pad it with dashes: %"--6s\n", "foo", "foo");
?>
printf: %s
<html>
<head>
<title>printf: %s</title>
</head>
<body>
<?php
$number = 543;
printf("String: %s<br>", $number );
?>
</body>
</html>
printf: Specifying a Field Width
<?
print "<pre>";
printf("%20s\n", "Books");
printf("%20s\n", "CDs");
printf("%20s\n", "Games");
printf("%20s\n", "Magazines");
print "</pre>";
?>
printf: %x (lowercase x)
<html>
<head>
<title>printf: %x</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (lower): %x<br>", $number );
?>
</body>
</html>
printf: %X (uppercase X)
<html>
<head>
<title>printf: %X</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (upper): %X<br>", $number );
?>
</body>
</html>
Type Specifiers
/*
Specifier Description
d Display argument as a decimal number
b Display an integer as a binary number
c Display an integer as ASCII equivalent
f Display an integer as a floating-point number (double)
o Display an integer as an octal number (base 8)
s Display argument as a string
x Display an integer as a lowercase hexadecimal number (base 16)
X Display an integer as an uppercase hexadecimal number (base 16)
*/
Using printf() to Format a List of Product Prices
<html>
<head>
<title>Using printf() to format a list of product prices</title>
</head>
<body>
<?php
$products = Array("Pencil"=>"222.4",
"Cake"=>"4",
"Coffee table"=>80.6
);
print "<pre>";
printf("%-20s%23s\n", "Name", "Price");
printf("%"-43s\n", "");
foreach ( $products as $key=>$val )
printf( "%-20s%20.2f\n", $key, $val );
printf("</pre>");
?>
</body>
</html>