Re: What am I doing wrong?

From: Date: Thu, 17 Apr 2003 07:25:35 +0000
Subject: Re: What am I doing wrong?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-144464@lists.php.net to get a copy of this message
Floating points aren't stored as precisely as you would expect. 14.95 is stored something like 14.949999, so multiplying it by three doesn't exactly equal 44.849999. A quick hack is to do this: $monthly = 14.95; $qtrly = 44.85; if(($qtrly != 0) && ((string)$qtrly != (string)($monthly * 3))) echo "Quarterly Incorrect ".$qtrly." ".($monthly * 3); else echo "Quarterly correct ".$qtrly." ".($monthly * 3); "Warren Vail" <warren@vailtech.net> wrote in message news:00b801c304ad$b0a40b60$6432e90c@comcast.net... > Does anyone see what I am doing wrong here? > > <?php > > $monthly = 14.95; > $qtrly = 44.85; > > if($qtrly != 0 && $qtrly != ($monthly * 3)) > echo "Quarterly Incorrect ".$qtrly." ".($monthly * 3); > else echo "Quarterly correct ".$qtrly." ".($monthly * 3); > ?> > > yields the following; > > Quarterly Incorrect 44.85 44.85 > > Warren Vail > warren@vailtech.net >

« previous php.general (#144464) next »