Some Built In Functions for Numbers

 

Introduction.  In order to improve our overall functionality we need to learn about some of the functions that are all provided with PHP.  This also will help motivate you to learn to develop your own functions.

We'll make use of some simple forms and then process the user inputs in a processing script page.  The built in functions that we will make use of are listed in the following table.

 

Function Description
round($number) This function rounds a number to the nearest integer.
round($number,precision) This function rounds a number to the nearest number of decimal places determined by the precision.
ceil($number) This function rounds a number up to the nearest integer.
floor($number) This function rounds a number down to the nearest integer.
abs($number) This function returns the absolute value of a number.

 

In order to allow the user to generate the inputs we will first make a form page called functions_number.html.

 

<html>
<head>
<title>Some Builtin Functions for Working with Numbers</title>
</head>

<body bgcolor = "660000" text="cccccc">
<form action="handle_functions_number.php" method=post>
<center><H2>Please enter a number</H2></center>
<table align = center>
<tr>
<td><font size = 4 color=cccccc>Some Number:</font>
</td>
<td><input type=text name="txtNumber" size=4>
</td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

The processing script should be called handle_functions_number.php in order for the post to work.

 

<html>
<head>
<title>Echoing and Using Functions on the Inputted Information</title>
</head>

<body>
<?php
// obtaining the information from the form and displaying it
print "You entered <b>$txtNumber</b><BR>\n";
$roundNumber = round($txtNumber);
print "Rounding this number to the nearest integer gives <b>$roundNumber </b><BR>\n";
$round2Number = round($txtNumber,2);
print "Rounding this number to two decimal places gives <b>$round2Number </b><BR>\n";
$ceilingNumber = ceil($txtNumber);
print "Rounding this number up gives <b>$ceilingNumber </b><BR>\n";
$floorNumber = floor($txtNumber);
print "Rounding this number down gives <b>$floorNumber </b><BR>\n";
$absNumber = abs($txtNumber);
print "The absolute value of this number down is <b>$absNumber </b><BR>\n";

?>
</body>
</html>

 

The results of this processing on the number 47.1173 are contained in the following image.

 

 

Another Example.  Now we want to work with some calculations and do a bit more formatting of the printing after the decimal places.  In this example we are going to compute the total cost of a purchase based on the
  • quantity purchased
  • cost per unit
  • tax rate

Because of having three inputs the form is a bit longer.  The form page should be called number_form_format.html.

 

<html>
<head>
<title>Number Form</title>
</head>

<body bgcolor = "660000" text="cccccc">
<form action="handle_number_form_format.php" method=post>
<center><H2>Please enter the following information</H2></center>
<table align = center>
<tr>
<td><font size = 4 color=cccccc>Quantity:</font>
</td>
<td><input type=text name="txtQuantity" size=4>
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Price:</font>
</td>
<td><input type=text name="txtPrice" size=4>
</td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Tax Rate:</font>
</td>
<td><input type=text name="txtTaxRate" size=50>
</td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

The processing script should be called handle_number_form_format.php in order for the post to work.

 

<html>
<head>
<title>Echoing and Computing with the Inputted Information</title>
</head>

<body>
<?php
// obtaining the information from the form and displaying it
print "The order quantity is <b>$txtQuantity</b><BR>\n";
print "The price per item is <b>\$$txtPrice </b><BR>\n";
print "The tax rate is <b>$txtTaxRate </b><BR>\n";
$txtTaxRate = $txtTaxRate + 1.0;
$total_cost = $txtQuantity * $txtPrice;
$total_cost = $total_cost * $txtTaxRate;
print "<BR><BR>Your total cost including tax is <b>\$";
printf("%01.2f",$total_cost);
// if we had used the round function and then echo it
// you can get zeros to drop off

$total_cost = round($total_cost,3);
print "<BR><BR></b>Your total cost including tax when rounded to 2 decimals is <b>\$";
echo $total_cost;
print"</b>";
?>
</body>
</html>

 

The calculations aren't particularly sophisticated.  The only particularly new piece of code relates to restricting the results of the calculation to two decimal places.  This is done with the printf( ) function.

printf("%01.2f",$total_cost);

The "%01.2f" restricts the output to two places after the decimal point.

You should also notice how we have used an escape character to get the $ to appear in the output.

I have also made use of the round( ) function in order to show you how some decimal places can be dropped when displayed.  You also want to try and see if its results differ from the printf results when there are more decimal places.