Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Formatting Numbers

Although the calculator is on its way to being practical, it still has one legitimate problem: You can’t ask someone to make a monthly payment of $10.13183333! To create more usable numbers, you need to format them.

There are two appropriate functions for this purpose. The first, round(), rounds a value to a specified number of decimal places. The function’s first argument is the number to be rounded. This can be either a number or a variable that has a numeric value. The second argument is optional; it represents the number of decimal places to which to round. If omitted, the number will be rounded to the nearest integer. For example:

round (4.30); // 4
round (4.289, 2); // 4.29
$num = 236.26985;
round ($num); // 236

The other function you can use in this situation is number_format(). It works like round() in that it takes a number (or a variable with a numeric value) and an optional decimal specifier. This function has the added benefit of formatting the number with commas, the way it would commonly be written:

number_format (428.4959, 2); // 428.50
number_format (428, 2); // 428.00
number_format (123456789); // 123,456,789

Let’s rewrite the PHP script to format the numbers appropriately.

To format numbers:

  1. Open handle_calc.php in your text editor or IDE, if it is not already open (Script 4.2).
  2. After all the calculations but before the print statement, add the following (Script 4.3):
    $total = number_format ($total, 2);
    $monthly = number_format ($monthly, 2);

    Script 4.3. The number_format() function is applied to the values of two number variables, so they are more appropriate.

    1    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    2       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    3    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    4    <head>
    5       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    6       <title>Product Cost Calculator</title>
    7       <style type="text/css" media="screen">
    8          .number { font-weight: bold;}
    9       </style>
    10   </head>
    11   <body>
    12   <?php // Script 4.3 - handle_calc.php #2
    13   /* This script takes values from calculator.html and performs
    14   total cost and monthly payment calculations. */
    15
    16   // Address error handling, if you want.
    17
    18   // Get the values from the $_POST array:
    19   $price = $_POST['price'];
    20   $quantity = $_POST['quantity'];
    21   $discount = $_POST['discount'];
    22   $tax = $_POST['tax'];
    23   $shipping = $_POST['shipping'];
    24   $payments = $_POST['payments'];
    25
    26   // Calculate the total:
    27   $total = $price * $quantity;
    28   $total = $total + $shipping;
    29   $total = $total − $discount;
    30
    31   // Determine the tax rate:
    32   $taxrate = $tax/100;
    33   $taxrate = $taxrate + 1;
    34
    35   // Factor in the tax rate:
    36   $total = $total * $taxrate;
    37
    38   // Calculate the monthly payments:
    39   $monthly = $total / $payments;
    40
    41   // Apply the proper formatting:
    42   $total = number_format ($total, 2);
    43   $monthly = number_format ($monthly, 2);
    44
    45   // Print out the results:
    46   print "<p>You have selected to purchase:<br />
    47   <span class=\"number\">$quantity</span> widget(s) at <br />
    48   $<span class=\"number\">$price</span> price each plus a <br />
    49   $<span class=\"number\">$shipping</span> shipping cost and a <br />
    50   <span class=\"number\">$tax</span> percent tax rate.<br />
    51   After your $<span class=\"number\"> $discount</span> discount, the total cost is
    52   $<span class=\"number\">$total</span>.<br />
    53   Divided over <span class=\"number\">$payments</span> monthly payments, that would be $<span class=\"number\">$monthly</span> each.</p>";
    54
    55   ?>
    56   </body>
    57   </html>

    To format these two numbers, apply this function after every calculation has been made but before they’re sent to the Web browser. The second argument (the 2) indicates that the resulting number should have exactly two decimal places; this setting rounds the numbers and adds zeros at the end, as necessary.

  3. Save the file, place it in the same directory as calculator.html, and test it in your browser circle-a.jpg and circle-b.jpg.
  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus