PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
In this example, $answer_01 = 175 and $answer_02 = 174...any ideas why ? I'm sure this is some totally obvious answer, but it doesn't make since to me.
Any help would be appreciated.
The value in $answer_01 is a float value. It may actually be 174.999999 because floats are not exact. intval() will take the integer part of it which is 174. You should consider casting the value to (int)$answer_02 or perhaps use ceil($answer_02) or round($answer_02). Please test these first because I haven't.
This problem is resolved for this application. Using the (int) cast was ineffective. The goal being to establish whether $price was evenly divisable by $divisor. Thanks to all those that made suggestions. The resolution is as follows:
$price = .0875; // This could be any number
$divisor = .0005; // This can be any number
$answer_01 = (float)($price/$divisor);
$answer_02 = ceil($answer_01);
if ($answer_01 != $answer_02){
echo "$price is NOT divisable by $divisor";
}else{
echo "$price is evenly divisable by $divisor";
}
jmashore wrote:This problem is resolved for this application. Using the (int) cast was ineffective. The goal being to establish whether $price was evenly divisable by $divisor.
If that's all you need to do, then why not use the modulus operator?