|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-05-02 20:43 UTC] zero at infernotechnologies dot net
Description:
------------
floatval() two seperate strings to return a result, try result against exact same number (via >=) and condition returns false.
Reproduce code:
---------------
$value1 = '1';
$value2 = '0.9';
$value3 = 0.1;
$result = floatval(floatval($value1) - floatval($value2));
if ($result >= $value3)
{
echo 'Working as intended';
}
else
{
echo "$result IS NOT >= $value3<br />";
var_dump($result, $value3);
}
Expected result:
----------------
Working as intended
Actual result:
--------------
0.1 IS NOT >= 0.1
float(0.1) float(0.1)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
It seems adding a single line fixes the issue: Line to add: --------------------- $result = floatval($result . ' '); Working version: --------------------- $value1 = '1'; $value2 = '0.9'; $value3 = 0.1; $result = floatval(floatval($value1) - floatval($value2)); $result = floatval($result . ' '); if ($result >= $value3) { echo 'Working as intended'; } else { echo "$result IS NOT >= $value3<br />"; var_dump($result, $value3); }$v = 'string'; $f = floatval($v); if ($f == $v) { echo "$f == $v" . PHP_EOL; } else { echo 'not equals' . PHP_EOL; }