I have a PHP function as
function func(){
function round_up($value, $decPlaces) {
return ceil($value * pow(10, $decPlaces)) / pow(10, $decPlaces);
}
$a=21.31;
$b=2;
$c=10.64;
$d=0.03;
$xxx=$a;
$yyy=round_up($b*$c,2)+round_up($d,2);
$zzz=($xxx===$yyy);
var_dump($xxx,$yyy,$zzz);
}
This function outputs
float(21.31) float(21.31) bool(false)
It seems xxx equal to yyy but why zzz is false? Where is the problem in this function and result?
Just change last line to this to see difference:
var_dump(number_format($xxx, 20), number_format($yyy, 20), $zzz);
Here are some helpful info about the problem:
https://stackoverflow.com/a/3148991/4180451 https://secure.php.net/manual/en/language.types.float.php
https://andy-carter.com/blog/don-t-trust-php-floating-point-numbers-when-equating
and many more... :)