In_array ignores 000 after decimal

$arr = array(2.1,3.1); if(in_array(2.1000,$arr)) echo "yes"; else echo "no"; 

I want it to show "No", but it ignores 0 after the decimal point.

+4
source share
3 answers

What you can do is (string)$arr[0] == '2.1000' . The only problem is that when using a floating-point number in PHP, it is going to “delete” the leading and trailing zeros, so they will always be non-zeros unless you store them as strings initially or if you keep track of leading and trailing zeros in another array.

+4
source

2.1 will always be equal to 2.1000 , since they represent the same value (they simply differ in the representation, which is lost as soon as PHP parses the number). You need to save at least one value as the string '2.1000' in order to get such a “comparison of views”.

0
source

The internal floating-point representation of 2.1000 exactly the same as 2.1 , so the code cannot tell the difference.

We should always know the difference between the internal representation of a numerical value and the representation that we use in the code or see in the output.

0
source

All Articles