-1

What are the possible odds of this value not entering the condition?

$a = 106709.71; //value in first table
$b = 106709.71; //value in second table

They're both Decimal in table.

I have the following code:

$a != $b ? '' : 'disabled';

It always returns the disabled one.

I have tried replicating it on PHP Emulator, but it worked even the other one is declared as string '106709.71' == 106709.71. It returns true.

I tried displaying the data on the front end and it's the same, they're both 106709.71.

My assumption is that the other one is displayed without a comma but the comma is there.

I trim the comma on both values using rtrim($var) and it works.

Any thoughts on why this is happening? Is it possible that it's displayed in the front and back end without comma but in reality there's a comma?

Benjoe
  • 466
  • 5
  • 20

2 Answers2

1

I think you are facing the issue of comparing decimals. It is not that straightforward. In https://www.php.net/manual/en/language.types.float.php there is a good description as to why you should not attempt a direct comparison and use other methods:

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

For simple explanation on how to do that you can refer to Compare decimals in PHP answer. And this whole website: Floating Point Guide goes from simple to deep on why that happens and how to remedy that.

P.S. : In your question, there is a typo $a != b ? '' : 'disabled';. It must be $a != $b ? '' : 'disabled'; That could also cause problems :)

Community
  • 1
  • 1
Evgenii Klepilin
  • 695
  • 1
  • 8
  • 21
  • The typo error is just in my example here. Thanks for noticing :) But the decimal value is inserted to the database is always in 2 decimal places. How is that possible? – Benjoe Mar 05 '20 at 04:36
  • Have you checked whether your database rounds it to two decimals? It would all depend on the type of field that you have assigned to that cell. There is a question https://stackoverflow.com/questions/13501459/decimal-datatype-is-rounding-the-values that answers some of that – Evgenii Klepilin Mar 05 '20 at 04:46
0

I have tried your case and i get the desired output:

$a = 106709.71; //value in first table
$b = 106709.71; //value in second table

$result = ($a != $b) ? 'Not Equal' : 'Equal'; //return FALSE because it is equal

echo $result;

Output

Equal

If i do a change in comparison like:

$a = 106709.71; //value in first table
$b = 106709.71; //value in second table

$result = ($a == $b) ? 'Not Equal' : 'Equal'; //returns TRUE

echo $result;

Output

Not Equal
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16