Using Decimal and Comma Separators
When it comes to formatting numeric values in PHP, there are only two functions: number_format () and sprintf (). Of these, the former is easier to understand and use, so let's begin with that function.
The number_format () function is used to display large numbers with comma and decimal separators. It can be used to control both the visibility and the appearance of the decimal digits, as well as the character used as the thousands separator. To see how this works, consider the following table:
mysql> SELECT accountNumber, accountName, J
accountBalance FROM accounts;
| accountNumber | accountName | accountBalance | +---------------+-------------+----------------+
mysql> SELECT accountNumber, accountName, J
accountBalance FROM accounts;
| accountNumber | accountName | accountBalance | +---------------+-------------+----------------+
|
1265489921 |
James D | |
2346 |
00000 |
|
2147483647 |
Timothy J | |
56347 |
50000 |
|
5739304575 |
Harish K | |
996564 |
87500 |
|
2173467271 |
Kingston X | |
634238 |
00000 |
|
2312934021 |
Sue U | |
34 |
67000 |
|
1248954638 | |
Ila T | |
5373 |
81982 |
|
2384371001 | |
Anil V | |
72460 |
00000 |
|
9430125467 | |
Katrina P | |
100 |
00000 |
|
1890192554 | |
Pooja B | |
17337 |
11914 |
|
2388282010 | |
Sue U | |
388883 |
12500 |
|
2374845291 | |
Jacob N | |
18410 |
00000 |
Here's a PHP script that displays this information on a web page, using number_format() to display account balances with two decimal places and commas as thousand separators:
// open connection to MySQL server
$connection = mysql_connect('localhost', 'guest', 'pass') J or die ('Unable to connect!');
// select database for use mysql_select_db('db2') or die ('Unable to select database!'); // create and execute query
$query = "SELECT accountNumber, accountName, accountBalance J FROM accounts";
$result = mysql_query($query) J
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned if (mysql_num_rows($result) > 0) {
echo '<table border=1 cellpadding=10>';
echo '<tr><td>Number</td><td>Name</td><td>Balance</td></tr>' // iterate over record set while($row = mysql_fetch_object($result)) {
echo '<td>' . $row->accountNumber . '</td>'; echo '<td>' . $row->accountName . '</td>'; echo '<td align=right>' . J number_format($row->accountBalance, 2, '.', ',') . '</td>'; echo '</tr>';
// print error message echo 'No rows found!';
// once processing is complete // free result set mysql_free_result($result);
// close connection to MySQL server mysql_close($connection);
Figure 15-6 shows the output of this script. Notice how the use of a comma separator significantly increases the readability of the numbers.
|
3 http://localhost/hide/! 5-OG.php - Microsoft Internet Explorer |
B | |||||||||
|
j j File Edit View Favorites Tools Help |
Back Forward Stop Refresh Home |
Search |
Favorites |
History |
» | |||||
|
j Address http://localhost/htde/15-OG.php |
_m*> |
Go |
J Links | | |||||||
|
Number |
Name |
Balance |
|
1265489921 |
James D |
2,346.00 |
|
2147483647 |
Timothy J |
56,347.50 |
|
5739304575 |
HarishK |
996,564.88 |
|
2173467271 |
Kingston X |
634,238.00 |
|
2312934021 |
Sue U |
34.67 |
|
1248954638 |
Ha T |
5,373.82 |
|
2384371001 |
Anil V |
72,460.00 |
|
Q/nm O^/IST |
FIGURE 15-6 Formatting numbers with the number_format() function You've already used the echo() function extensively to display output. However, echo() doesn't let you format output in any significant manner, for example, you can't write 1 as 00001.00. So, another common function used to perform this type of number formatting is the sprintf() function, which enables you to define the format in which data is output. Consider the following example: // returns 1.6666666666667 print(5/3); As you might imagine, that's not very friendly. Ideally, you'd like to display just the significant digits of the result, so you'd use the sprintf() function, as in the following: The PHP sprintf() function is similar to the sprintf() function that C programmers are used to. To format the output, you need to use field templates, templates that represent the format you'd like to display. Common field templates are listed in Table 15-1. You can also combine these field templates with numbers that indicate the number of digits to display—for example, %1.2f implies that PHP should only display two digits after the decimal point. If you'd like the formatted string to have a minimum length, you can tell PHP which character to use for padding by prefixing it with a single quote ('). Template What It Represents %s %d %x %o %f string decimal number hexadecimal number octal number float number
Common Field Templates Supported by the sprintf() Function |
Post a comment