Problem with rounding in intval

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!

Moderator: General Moderators

Post Reply
jmashore
Forum Newbie
Posts: 2
Joined: Tue Mar 24, 2009 11:54 am

Problem with rounding in intval

Post by jmashore »

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.

Code: Select all

$price = .0875;
$divisor = .0005;
 
$answer_01 = (float)($price/$divisor);
$answer_02 = intval($answer_01);
 
echo $answer_01 . "<br>";
echo $answer_02;
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Problem with rounding in intval

Post by wellhole »

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.

See the references at http://us3.php.net/manual/en/function.round.php
jmashore
Forum Newbie
Posts: 2
Joined: Tue Mar 24, 2009 11:54 am

Re: Problem with rounding in intval

Post by jmashore »

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:

Code: Select all

$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";
}
 
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Problem with rounding in intval

Post by wellhole »

I don't think division guarantees 2 / 2 = 0.999999. You might get 2 / 2 = 1.000001. ceil() on each of those will produce 2 different values.
mmoussa
Forum Newbie
Posts: 8
Joined: Wed Mar 18, 2009 8:43 am

Re: Problem with rounding in intval

Post by mmoussa »

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?

Code: Select all

 
if ( $price % $divisor == 0 )
{
    // do stuff that you do when they are evenly divisible
}
else
{
    // do stuff that you do when they are not
}
 
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Problem with rounding in intval

Post by wellhole »

mmoussa wrote:If that's all you need to do, then why not use the modulus operator?
Mod doesn't work with floats. Perhaps ceil() may very well be the best option. Let's just hope 2 / 2 will always be .999999 and never 1.000001.
Post Reply