On Thu, 2010-05-06 at 11:24 +0100, Paul Waring wrote:

> I've got the following script which demonstrates a problem I'm having 
> with floating point numbers and intval:
> 
> $times100 = (-37.12 * 100);
> print $times100 . "\n";
> 
> $intval100 = intval($times100);
> print $intval100 . "\n";
> 
> print ($intval100 / 100) . "\n";
> 
> I expect the output to be:
> -3712
> -3712
> -37.12
> 
> However, the actual output I'm getting (on several systems, so it's not 
> just my machine) is:
> -3712
> -3711
> -37.11
> 
> Is there a reason for this, and a better way I should be doing it? 
> Basically I need to add up a list of floats, representing currency 
> values, and see if they're equal to another float, but I need to convert 
>   them to ints first because I can't guarantee than there won't be 
> something after the second decimal place.
> 
> Thanks,
> 
> Paul
> 
> -- 
> Paul Waring
> http://www.pwaring.com
> 


It's part of the rounding problem you get with most languages out there.
Why can't you compare the floating point values though? Currency should
only have one decimal place anyway.

Thanks,
Ash
http://www.ashleysheridan.co.uk


Reply via email to