Ensure a variable is divisible by 4

Larry Bates larry.bates at websafe.com
Mon Dec 4 12:16:57 EST 2006


geskerrett at hotmail.com wrote:
> I am sure this is a basic math issue, but is there a better way to
> ensure an int variable is divisible by 4 than by doing the following;
> 
> x = 111
> x = (x /4) * 4
> 
> Just seems a bit clunky to me.
> 
Use modulo operator '%'

if not x % 4:
   #
   # Arrive here if x is modulo 4 divisable
   #

-Larry Bates



More information about the Python-list mailing list