RE: [PHP] round a number down to an odd number

From: Date: Mon, 11 Dec 2000 15:47:25 +0000
Subject: RE: [PHP] round a number down to an odd number
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-29699@lists.php.net to get a copy of this message
> -----Original Message----- > From: Steve Werby [mailto:steve@247computing.com] > Sent: Sunday, December 10, 2000 1:53 PM > To: Tom Beidler; php list > Subject: Re: [PHP] round a number down to an odd number > > >> I have a page where I'm trying to swap out images to represent >> a product. There's one image for every 2 products id's. Is there >> a way in PHP to return an odd number? That way I can name the >> images after the odd part numbers, if someone orders the small >> size which would have the even part number, I could have the >> formula knock off a number, and use the odd part number image. If >> they would order the large the formula wouldn't knock off a number. > > Use the modulus operator "%" which returns a remainder. > > if( $num % 2 == 0 ) // $num is an even number > { > $num = $num - 1; > } Or, if you want something a little more compact, just... $num = $num - (($num+1)%2); If $num is odd, then it'll be unchanged, since (oddnumber+1)%2 will be zero. If $num is even, it'll be reduced by 1. --- Mark Roedel | "The most valuable function performed by Systems Programmer | the federal government is entertainment" LeTourneau University | Longview, Texas, USA | -- Dave Barry

« previous php.general (#29699) next »