Formatting Numbers Using round( ) And number_format( )
If you have ever work in excel you know that you need to format numbers either from automatic calculations or typing them in. Well, that is possible in PHP with the use of two functions, round( ) and number_format( ). If you can remember back in your early school days, you were taught about rounding up or rounding down with anything lower then 5 gets rounded down while 5 and above get rounded up. On top of that you can set up this function to tell how many decimal places you want PHP to round that number.
EXAMPLE
round (2.36945, 2);
In the above example we are telling PHP to round 2.36945 and have the decimal move to two places and so your end result should be 2.37, since 9 is the last number the PHP will round it up instead of down. Although both round( ) and number_format( ) have the same function, number_format( ) has the added bones of adding a comma if it is a large number.
EXAMPLE
number_format (123456789); when running should look like 123,456,789 when it is printed.
So, with using the form from the previous example I will be leaving it unformatted and so I would suggest opening a new window to compare the unformatted and formatted numbers thanks to those two functions.
body {
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
}
p, h1, form, button {
border:0;
margin:0;
padding:0;
}
.spacer {
clear:both;
height:1px;
}
/* ----------- My Form ----------- */
.myform {
margin:0 auto;
width:400px;
padding:14px;
}
/* ----------- stylized ----------- */
#stylized {
border:solid 2px #b7ddf2;
background:#ebf4fb;
}
#stylized h1 {
font-size:14px;
font-weight:bold;
margin-bottom:8px;
}
#stylized p {
font-size:11px;
color:#666666;
margin-bottom:20px;
border-bottom:solid 1px #b7ddf2;
padding-bottom:10px;
}
#stylized label {
display:block;
font-weight:bold;
text-align:right;
width:140px;
float:left;
}
#stylized .small {
color:#666666;
display:block;
font-size:11px;
font-weight:normal;
text-align:right;
width:140px;
}
#stylized input {
float:left;
font-size:12px;
padding:4px 2px;
border:solid 1px #aacfe4;
width:200px;
margin:2px 0 20px 10px;
}
#stylized select {
float:left;
font-size:12px;
padding:4px 0px;
border:solid 1px #aacfe4;
width:200px;
margin:2px 0 20px 10px;
}
#stylized button {
clear:both;
margin-left:150px;
width:125px;
height:31px;
background:#666666 text-align:center;
line-height:31px;
color:#FFFFFF;
font-size:11px;
font-weight:bold;
}
<div id="stylized" class="myform">
<form id="form" name="form" method="post" action="handle_calc.php">
<h1>Order Form</h1>
<p>Fill out this form to calculate the total cost:</p>
<label>Price <span class="small">Add Price of Product</span> </label>
<input type="text" name="price" size="5" />
<label>Quantity: <span class="small">How many do you Want?</span> </label>
<input type="text" name="quantity" size="5" />
<label>Discount: <span class="small">Discount Special</span> </label>
<input type="text" name="discount" size="5" />
<label>Shipping method: <span class="small">Min. size 6 chars</span> </label>
<select name="shipping">
<option value="5.00">Slow and steady</option>
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select>
<label># of Payments: <span class="small">Number of payments </span> </label>
<input type="text" name="payments" size="5" />
<input type="submit" name="submit" value="Calculate!" />
<div class="spacer"></div>
</form>
</div>
As for the actual PHP most of the script is fine except you have to do two things to it, the first one is to add the number format function and of course update the output to make sure it display the proper number format.
// Apply the proper formatting. $total = number_format ($total, 2); $monthly = number_format ($monthly, 2); // Print out the results: print " You have selected to purchase: $quantity widget(s) at $$price price each plus a $$shipping shipping cost and a $tax percent tax rate. After your $$discount discount, the total cost is $$total. Divided over $payments monthly payments, that would be $$monthly each. ";
Now you seen the unformatted numbers and so now if you run to put in the same data and run the php script, those unformatted numbers will have decimal and maybe some commas in it as well.
<div id="stylized" class="myform">
<form id="form" name="form" method="post" action="handle_calc2.php">
<h1>Order Form</h1>
<p>Fill out this form to calculate the total cost:</p>
<label>Price <span class="small">Add Price of Product</span> </label>
<input type="text" name="price" size="5" />
<label>Quantity: <span class="small">How many do you Want?</span> </label>
<input type="text" name="quantity" size="5" />
<label>Discount: <span class="small">Discount Special</span> </label>
<input type="text" name="discount" size="5" />
<label>Shipping method: <span class="small">Min. size 6 chars</span> </label>
<select name="shipping">
<option value="5.00">Slow and steady</option>
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select>
<label># of Payments: <span class="small">Number of payments </span> </label>
<input type="text" name="payments" size="5" />
<input type="submit" name="submit" value="Calculate!" />
<div class="spacer"></div>
</form>
</div>
Formatted Numbers Script
// Address error handling, if you want. // Get the values from the $_POST array: $price = $_POST['price']; $quantity = $_POST['quantity']; $discount = $_POST['discount']; $tax = $_POST['tax']; $shipping = $_POST['shipping']; $payments = $_POST['payments']; // Calculate the total: $total = $price * $quantity; $total = $total + $shipping; $total = $total - $discount; // Determine the tax rate: $taxrate = $tax/100; $taxrate = $taxrate + 1; // Factor in the tax rate: $total = $total * $taxrate; // Calculate the monthly payments: $monthly = $total / $payments; // Apply the proper formatting. $total = number_format ($total, 2); $monthly = number_format ($monthly, 2); // Print out the results: print "<div>You have selected to purchase:<br /> <span class="number">$quantity</span> widget(s) at <br /> $<span class="number">$price</span> price each plus a <br /> $<span class="number">$shipping</span> shipping cost and a <br /> <span class="number">$tax</span> percent tax rate.<br /> After your $<span class="number">$discount</span> discount, the total cost is $<span class="number">$total</span>.<br /> Divided over <span class="number">$payments</span> monthly payments, that would be $<span class="number">$monthly</span> each.</p></div>";
That is it for this tutorial and now you know two more functions to add to you PHP skills.













Leave your response!
You must be logged in to post a comment.