PDA

View Full Version : How to check to see if a number is divisable by 1000

thekevin07
06-20-2007, 04:57 AM
HI

Does anyone know how to check to see if a number is divisible by 1000 like for example if the num being checked is 2000 it is true or 1 and if its 2001 its false or 0

Any one know how to do this?

Thanks

kbluhm
06-20-2007, 05:06 AM
<?php

\$divisible = ( \$num % 1000 == 0 );

if ( \$divisible )
echo 'exactly divisible by 1000';
else
echo 'not exactly divisible by 1000, we have a remainder';

?>

_Aerospace_Eng_
06-20-2007, 05:52 AM
Or by using a ternary operator

\$divisible = (\$num % 1000 == 0) ? 'exactly divisible by 1000' : 'not exactly divisible by 1000, we have a remainder';
echo \$divisible;

kbluhm
06-20-2007, 06:03 AM
While that is true, the if-else in my example was intended to demonstrate the boolean result... answering the query of the intended result and not the display text. Besides, swapping an if-else for a ternary operator is fairly redundant when giving examples. You've posted an identical solution. :thumbsup:

_Aerospace_Eng_
06-20-2007, 07:22 AM
Yes I know I posted an identical solution. I was just posting a shorter method. The key part to the solutions given is the modulus operator. It divides one number into another and leaves a remainder. If the remainder is greater than 0 then the two numbers aren't divisible by each other.