PHP/Development/Printf
Содержание
- 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
<source lang="html4strict">
<?php printf ("left-pad numbers with zeroes: %05f\n", 33.22); ?>
</source>
printf: %b
<source lang="html4strict">
<html>
<head>
<title>printf: %b</title>
</head>
<body>
<?php
$number = 543;
printf("Binary: %b
", $number );
?>
</body>
</html>
</source>
printf: %d
<source lang="html4strict">
<html>
<head>
<title>printf: %d</title>
</head>
<body>
<?php
$number = 543;
printf("Decimal: %d
", $number );
?>
</body>
</html>
</source>
printf: %d, %c
<source lang="html4strict">
<?php printf ("The character value of %d is %c\n", 72, 72); ?>
</source>
printf: %f
<source lang="html4strict">
<html>
<head>
<title>printf: %f</title>
</head>
<body>
<?php
$number = 543;
printf("Double: %f
", $number );
?>
</body>
</html>
</source>
printf %f,%10f,%-010f,%2.2f
<source lang="html4strict"> :
<?php $value = 3.14159; printf("%f,%10f,%-010f,%2.2f\n", $value, $value, $value, $value); ?>
</source>
printf: %f, %.2f
<source lang="html4strict">
<?php printf ("Control the number of decimals in %f with %.2f\n", 5.1234, 5.1234); ?>
</source>
printf: hexadecimal representation %x
<source lang="html4strict">
<?php printf ("The hexadecimal representation of %d is %x\n", 92, 92); ?>
</source>
printf: left-pad a number and specify precision: %05.2f
<source lang="html4strict">
<?php printf ("left-pad a number and specify precision: %05.2f\n", 33.22); ?>
</source>
printf: Left-pad the string %s, %-6s
<source lang="html4strict">
<?php printf ("Left-pad the string %s with dashes: %"-6s\n", "foo", "foo");
?>
</source>
printf: %o
<source lang="html4strict">
<html>
<head>
<title>printf: %o</title>
</head>
<body>
<?php
$number = 543;
printf("Octal: %o
", $number );
?>
</body>
</html>
</source>
printf: octal representation %o
<source lang="html4strict">
<?php printf ("octal representation is %o)\n", 92);
?>
</source>
printf: right-pad with dashes: %"--6s
<source lang="html4strict">
<?php printf ("right-pad it with dashes: %"--6s\n", "foo", "foo");
?>
</source>
printf: %s
<source lang="html4strict">
<html>
<head>
<title>printf: %s</title>
</head>
<body>
<?php
$number = 543;
printf("String: %s
", $number );
?>
</body>
</html>
</source>
printf: Specifying a Field Width
<source lang="html4strict">
<?
print ""; printf("%20s\n", "Books"); printf("%20s\n", "CDs"); printf("%20s\n", "Games"); printf("%20s\n", "Magazines"); print "";
?>
</source>
printf: %x (lowercase x)
<source lang="html4strict">
<html>
<head>
<title>printf: %x</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (lower): %x
", $number );
?>
</body>
</html>
</source>
printf: %X (uppercase X)
<source lang="html4strict">
<html>
<head>
<title>printf: %X</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (upper): %X
", $number );
?>
</body>
</html>
</source>
Type Specifiers
<source lang="html4strict">
/* 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)
- /
</source>
Using printf() to Format a List of Product Prices
<source lang="html4strict">
<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 ""; printf("%-20s%23s\n", "Name", "Price"); printf("%"-43s\n", ""); foreach ( $products as $key=>$val ) printf( "%-20s%20.2f\n", $key, $val ); printf("");
?>
</body>
</html>
</source>