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.

 Some Builtin Functions for Working with Numbers

Some Number:

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

 Echoing and Using Functions on the Inputted Information \$txtNumber
\n"; \$roundNumber = round(\$txtNumber); print "Rounding this number to the nearest integer gives \$roundNumber
\n"; \$round2Number = round(\$txtNumber,2); print "Rounding this number to two decimal places gives \$round2Number
\n"; \$ceilingNumber = ceil(\$txtNumber); print "Rounding this number up gives \$ceilingNumber
\n"; \$floorNumber = floor(\$txtNumber); print "Rounding this number down gives \$floorNumber
\n"; \$absNumber = abs(\$txtNumber); print "The absolute value of this number down is \$absNumber
\n"; ?>

 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.

 Number Form

Quantity:
Price:
Tax Rate:

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

 Echoing and Computing with the Inputted Information \$txtQuantity
\n"; print "The price per item is \\$\$txtPrice
\n"; print "The tax rate is \$txtTaxRate
\n"; \$txtTaxRate = \$txtTaxRate + 1.0; \$total_cost = \$txtQuantity * \$txtPrice; \$total_cost = \$total_cost * \$txtTaxRate; print "

Your total cost including tax is \\$"; 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 "

Your total cost including tax when rounded to 2 decimals is \\$"; echo \$total_cost; print""; ?>

 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.