0

I have a very strange behavior in php with float. I set 2 vars $finalttc and $totalTtc with a function. this function is the following

$finalttc = ttc(rem(3584.03,30),10);  
$totalTtc=  ttc(rem(3584.03,30),10);

the 3584.03 are set from differents sources but this is the same datas fron database... (a sum of numeric lines)

here are the functions

function ttc($tarifHt, $tva) {
       $ttc = round (  ((($tarifHt/100)*$tva) + $tarifHt), 3) ;           
    return round (  $ttc, 2,PHP_ROUND_HALF_EVEN); 
}

 function rem($prix,$remise=0) { 
     if ($remise == 0) {
         return $prix;
     } else {         
          $r = round( ($prix-(($prix/100)*$remise))  ,3 );
         $r = round( $r , 2,PHP_ROUND_HALF_ODD );
        return $r;
     }
}

I test like this

 echo 'diff ttc '.((float)$finalttc).' '.(float)$totalTtc.'<br />';
 var_dump(((float)$finalttc) != ((float)$totalTtc) );

 echo 'diff ttc trim'.trim((float)$finalttc).' '.(float)$totalTtc.'<br />';
 var_dump(trim((float)$finalttc) != trim((float)$totalTtc) ); 

 echo  base64_encode((float)$finalttc).' '.base64_encode((float)$totalTtc).'<br />';                
 var_dump(base64_encode((float)$finalttc) != base64_encode((float)$totalTtc));

 echo decbin((float)$finalttc).' '.decbin((float)$totalTtc).'<br />';
 var_dump(decbin((float)$finalttc) != decbin((float)$totalTtc));

And I GET this result

diff ttc 3584.03 3584.03
boolean true

diff ttc trim3584.03 3584.03
boolean false

MzU4NC4wMw== MzU4NC4wMw==
boolean false

111000000000 111000000000
boolean false

I really do not understand why the first test return true ????

thanks for your help

J. Doe
  • 119
  • 1
  • 8

0 Answers0