[thelist] PHP type and value match, but value comparssion fails.

Liam Delahunty liam at megaproducts.co.uk
Wed Aug 25 10:49:22 CDT 2004


on 25/08/2004 14:51 sbeam said the following:

> On Wednesday 25 August 2004 09:03 am, Liam Delahunty wrote:
> 
>>eg,
>>  3 units X 0.5 = 1.5, works
>>  3 units X 0.6 = 1.8, fails...
>>
>>So, what gives? How can I check these values, and what is causing the
>>errors?
> 
> http://us2.php.net/manual/en/language.types.float.php
> read the box about "Floating point precision" where it says "never 
> compare floating point numbers for equality" 
> 

Doh! I'd even tried making everything float in the real thing, but of 
course that's just making things worse!

For the purposes of my test, I was able to:
$mysum = $unit_price * $sold_by;
$mysum = number_format($mysum,2);
$price = number_format($price,2);
if ($price != $mysum){
	// blah
}

and it's okay now.

Thanks one and all.

-- 
Kind regards, Liam Delahunty, Mega Products Ltd
12 Bury Place, London WC1A 2JL Fax: +44(0)871 224 7891
http://www.megaproducts.co.uk/ Internet Design & Development


More information about the thelist mailing list