Error comparing two numbers

Asked

Viewed 92 times

3

I’m having a problem comparing two numbers, follow my code:

// Valor
    $total_ch = 691.11;

    // Calcula valor total das contas a receber
    $result = $mysqli->query("SELECT SUM(valor) FROM contas_receber where status = '1' and id IN ($contas)");
    $row = mysqli_fetch_array($result);
    $valor_contas =  $row['SUM(valor)'];

    // Verifica se o total dos cheques e maior que os das contas a receber
    if ($valor_contas > $total_ch) {
        // Exibe Alert
        echo "erro";
    }

The value returned from $row['SUM(valor)'] is 691.11. Therefore, the two values are equal, but the system is pointing out which value of valor_contas is greater than that of total_ch.

What is causing this? How to solve?

  • It may be that he is rounding up the value of $valor_contas. Send the BD structure. Another thing is that it will take the total value of everything that came from select, so we need to know which value it returns.

  • Got it. Well the value field is so valor decimal(10,2) DEFAULT NULL. I sent an echo in the two variables, and the value written on the screen are the same. what I’m not getting.

  • But get in the if?

  • yes come in, and write echo "erro";

  • Gives a var_dump() in both variables.

  • On the php website explain something about comparing float numbers http://php.net/manual/en/language.types.float.php

Show 1 more comment

1 answer

1


Cast the string to float using (float), thus:

$valor_contas =  (float) $row['SUM(valor)'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.