PHP Integer and Float Comparison Mismatch

php integer and float comparison mismatch

Notice the big red warning in the PHP Manual!

Never expect anything when comparing floats. The result of round, even if the precision is 0, is still a float. In your particular case it happened that the result was a little bigger than expected, so casting to int resulted in equality, but for other numbers it might as well happen for it to be a little smaller than expected and casting to int won't round it, but truncate it, so you can't use casting as a workaround. (As a note, a better solution than yours would be casting to string :), but still a lousy option.)

If you need to work with amounts of money always use the BC Math extension.

For rounding with BC Math you can use this technique:

$x = '211.9452';
$x = bcadd($x, '0.005', 2);

Good luck,

Alin

Comparing floats - same number, but does not equal?

floating point numbers have limited precision. view the warning about comparing them here:

http://php.net/manual/en/language.types.float.php

PHP fails with comparsion of floats and integers

i think your problem lies at elseif (($taxed >= 99) && ($total < 199)) {

it should be elseif (($taxed >= 99) && ($taxed < 199)) {

How to check if a number (float or integer) is within a range (0 - 100)

No need for regex:

if (is_numeric($val) && $val > 0 && $val <= 100)
{
echo '$val is number (int or float) between 0 and 100';
}

Demo

Update

It turns out you're getting the numeric values from a string. In that case, it would be best to extract all of them using a regex, something like:

if (preg_match_all('/\d+\.?\d*/', $string, $allNumbers))
{
$valid = [];
foreach ($allNumbers[0] as $num)
{
if ($num > 0 && $num <= 100)
$valid[] = $num;
}
}

You can leave out the is_numeric check, because the matched strings are guaranteed to be numeric anyway...

Compare floats in php

If you do it like this they should be the same. But note that a characteristic of floating-point values is that calculations which seem to result in the same value do not need to actually be identical. So if $a is a literal .17 and $b arrives there through a calculation it can well be that they are different, albeit both display the same value.

Usually you never compare floating-point values for equality like this, you need to use a smallest acceptable difference:

if (abs(($a-$b)/$b) < 0.00001) {
echo "same";
}

Something like that.

Less than operator not working correctly in PHP 5.3.1

If one of the floats is calculated numerically, and one is created from string assignment, they could be different. Try the following:

$x = 147.22;
$y = 147.2200000000001;
printf("%.40f\n", $x);
printf("%.40f\n", $y);
var_dump($x);
var_dump($y);
var_dump($x < $y);

outputs

147.2199999999999988631316227838397026062012
147.2200000000001125499693443998694419860840
float(147.22)
float(147.22)
bool(true)

Cast them to a string with a specified precision for comparison.

MySQL floating point comparison issues

Do you notice the problem below?

CREATE TABLE a (num float);

INSERT INTO a VALUES (50.12);
INSERT INTO a VALUES (34.57);
INSERT INTO a VALUES (12.75);
INSERT INTO a VALUES (11.22);
INSERT INTO a VALUES (10.46);
INSERT INTO a VALUES (9.35);
INSERT INTO a VALUES (8.55);
INSERT INTO a VALUES (7.23);
INSERT INTO a VALUES (6.53);
INSERT INTO a VALUES (5.15);
INSERT INTO a VALUES (4.01);

SELECT SUM(num) FROM a;
+-----------------+
| SUM(num) |
+-----------------+
| 159.94000005722 |
+-----------------+

There's an extra 0.00000005722 spread between some of those rows. Therefore some of those values will return false when compared with the value they were initialized with.

To avoid problems with floating-point arithmetic and comparisons, you should use the DECIMAL data type:

ALTER TABLE a MODIFY num DECIMAL(6,2);

SELECT SUM(num) FROM a;
+----------+
| SUM(num) |
+----------+
| 159.94 |
+----------+
1 row in set (0.00 sec)


Related Topics



Leave a reply



Submit