Re: Rounding arithmetic results
| From: | David Robley | Date: | Thu, 15 Jun 2000 00:56:06 +0000 |
| Subject: | Re: Rounding arithmetic results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-1895@lists.php.net to get a copy of this message | ||
On 14 Jun, Dion Vansevenant wrote:
> I am fairly new to PHP3 so please forgive me if this seems like to
> no-brainer to you.
>
> I am working on a little chunk of code that counts the number of pages in a
> given area, then divides by 2 to determine how many items to print in each
> of two columns of a table.
>
> Works just great when the results are based on an even number of pages, but
> odd numbers give me half answers (1.5, 2.5, etc.).
>
> What would be a good method of checking for a half answer and rounding up?
>
> Thanks.
>
> Dion
The modulus operator % is your friend here
if($numpages % 2 == 0):
// do the first column, it's an even number
else:
// it's odd, do the second column
endif;
Expand as necessary for a larger number of columns.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA