|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-09-04 14:30 UTC] email at brunofaria dot pt
Description:
------------
I'm adding this numbers from an array with key[0]:
array(4) { [0]=> array(2) { [0]=> string(4) "61.8" [1]=> string(11) " Poliéster" } [1]=> array(2) { [0]=> string(4) "20.1" [1]=> string(6) " Nylon" } [2]=> array(2) { [0]=> string(4) "11.4" [1]=> string(9) " Algodão" } [3]=> array(2) { [0]=> string(3) "6.7" [1]=> string(6) " Rayon" } }
61.8 + 20.1 + 11.4 + 6.7 = 100
Then I'm comparing to 100,
Returns False.
Test script:
---------------
$value = 0;
foreach ($array as $key) {
$value += floatval ( preg_replace ('/\s+/', '', $key[0]) ) ;
}
if ($value == 100 ) {
return true;
} else {
return false;
}
Expected result:
----------------
true
Actual result:
--------------
false
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
convert to strings is sonsense even if it works by luck, limited precision was already explained, the value is something like "100.0000000000000142108547152020037174224853515625" if(100 == (int)$value) if(100 === (int)$value) one of both would work <?php $value = 61.8 + 20.1 + 11.4 + 6.7; ini_set('precision', 1000); if(100 == (int)$value) { echo "true: $value\n"; } else { echo "false: $value\n"; } ?>