1

I'm trying to compare two floats in twig but I don't have the good result :

I have two 'equal' floats : float1 and float2

{% if float1 == float2 %}
   <span>Floats are equal</span>
{% else %}
   <span>Floats are different</span>
{% endif %}
{{ float1 == float2 }}

Display :

<span>Floats are different</span>
1

How can I compare two floats in Twig ? I don't understand why the result of the comparison is true but the result of the if statement is false

  • Possible duplicate of https://stackoverflow.com/questions/8364189/comparing-floats-same-number-but-does-not-equal. [PHP's documentation will also help you](http://php.net/manual/en/language.types.float.php). –  May 31 '17 at 15:16
  • I know how floats works but I don't understand why the result of the comparison is different in the if statement and when I display it. Also, is there a solution to compare floats in twig ? – Godineau Félicie May 31 '17 at 15:19
  • A look at the generated cache file might reveal why they're different. Also: You compare floats in twig like you do everywhere else: You check their relative difference for some acceptable epsilon and consider them equal if the difference is below that. – ccKep May 31 '17 at 15:23
  • I tried something like this : {% if (float1 - float2)|abs < 0.00001 %} But this still goes in the else statement – Godineau Félicie May 31 '17 at 15:26
  • 2
    Which version of Twig are you using? What are your values? I tried your example on [twigfiddle](https://twigfiddle.com/qyj0ld) and it behaves correctly. –  May 31 '17 at 15:27
  • Can you tell us some example values that you are comparing please? – Alvin Bunk May 31 '17 at 15:28
  • The values are returned from a function but when I print them, I have 304 and 304 I am using version 1.24.1, I tried with this version in your twigfiddle and it gave me the right answer ! Also, when I try with the direct values, it works too .. Is there something specific about returned values ? – Godineau Félicie May 31 '17 at 15:36

1 Answers1

0

Here is a working example that I tried on Twigfiddle:

{% set float1 = 1.0123456789012 %}
{% set float2 = 1.0123456789011 %}

{% if float1 == float2 %}
   <span>Floats are equal</span>
{% else %}
   <span>Floats are different</span>
{% endif %}

The limit (at least on Twigfiddle) is 13 decimal places. So 1.0123456789012 works; but if you increase to 1.01234567890123, the comparison won't work.

Here is the Twigfiddle for you to see it working: https://twigfiddle.com/r3hi48

Is your code any different than that? In other words in the above I use set to declare the two float variables. Have you also tried {{ dump(float1) }} to see what the value prints out as?

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • In fact, I noticed that my code works with numbers but I compare two floats returned by a function. Do you know if there is something specific about returned float values in Twig ? – Godineau Félicie May 31 '17 at 15:55
  • Do you mean you are comparing using a function in your Controller? If so, can you edit your post and show `some` of the relevant code? Or maybe you mean something else? Have you tried the `{{ dump(float1) }}` function to see what the actual values are? – Alvin Bunk May 31 '17 at 15:59
  • 2
    I passed to my template an intervention object, then I tried to compare the result of two functions (intervention.getPrice() == intervention.getPriceMin()) This gave me my comparison problem. the dump showed the same value for the two float I just had my template to work thanks to 'set', I set the return of my functions into variables and compare those variables. I think that I will delete this question and repost a question about why the returned values are not well interpreted Thanks for your answer btw ! It helped me to find the set trick – Godineau Félicie May 31 '17 at 16:11