Error in ABS PHP conversion?

Asked

Viewed 101 times

1

I have the following routine:

    $saldisp = -13.02; // Resultado de uma operação matemática
    $valorpagar = "13,02"; // Coloquei o valor real que vem do Edit

    $saldodisp = (float) abs($saldisp);

    $valorpagar = (float) $valorpagar;

    if ($valorpagar < 10) {
        fValida(__LINE__, "", "", "O valor mínimo para pagamento é de R$ 10,00");
    } else {
        if ($saldodisp < $valorpagar) { // Ele entra aqui?
            fValida(__LINE__, "", "", "Valor máximo para pagamento é de R$ " . ($saldodisp));
        }
    }

It is entering the second IF, but in IF the values are exactly equal 13.02

The only thing that differs is before the IF is that the saldodisp is negative (-13.02) and the value that comes from the Edit is positive (13.02)

But I’m converting the negative with the abs()

What may be missing yet?

Obs: Placing a debug on if the value of saldisp is 13.02 and Edit is 13.02

I can’t find where the mistake is.

We could say that it’s a conversion error when there are N decimal places, but then the debug would show me these places, and I’m not doing any division or multiplication, I’m simply taking the value of the bank converting with abs and comparing it with the value that comes from Edit

  • Please [Edit] and convert your code into a [mcve] so that we can test and help (doing so so that we can run the snippet checking the problem without relying on your DB, for validation of past statements).

  • I confess that I did not know the literal result of (float) 13.02, what is occurring is that (float) is converting in local currency 13.02 and by what it seems php is comparing two strings, by formatting the values back to 13.02 (replacing point by comma) it makes the correct comparison in if.

  • He improved his Dit, but to be a MCVE it is important that it causes the same problem as the original code, you have to check if what came from DB is string or number (example: $saldisp = '-13,02';) - but just like it comes from db

  • I forgot the minus sign, it comes from the bank so -13.02 and from Edit it comes 13.02, but when using the (float) in front it converts the values to 13.02 (with comma) it ends up giving problem in if that buys strings and not floats, I used the (float) because in the database there are values with more than 2 decimal places, but do not expect this type of conversion, I thought php would read as decimal.

  • 1

    You need to tell what kind of value you have in your database. Would it really float? Decimal? String? Float is a complicated thing, and is not recommended to deal with monetary values, for precision problems. For example, sometimes 2 values appear identical but are not, especially if one of them is the result of some mathematical operation. Example: https://ideone.com/EisV9g

  • Marcelo, to clarify, this question code is giving the same error? (I have no way to test now, I am on time). If you’re not making the mistake, you need to see what’s different from DB. If it is, it solved the question and is suitable for an answer (which may probably be one that already has on the website about comparison of float, the same problem that @bfavaretto mentioned).

  • In my bank is as decimal (12.4) which can be negative and in the example I passed the number returned from the bank is exactly (-13.02), but the problem was to use the function (float) to work numbers with more than 2 decimal places and make sure that strings (13.02) would be converted to decimal (13.02), as it turns into the local currency with the proper separators and this causes problem in comparison. The use of (float) was precautionary, but it brought me a problem, I already solved the issue. Then I will post.

  • Bacco, really the example of bfavaretto is in agreement... the result comes from an operation and this is causing the problem, in the example above I wanted to simplify, but I did not test... making the above example not error, thanks for more this. @bfavaretto put as an answer for me to accept.

  • 1

    Marcelo, I thought it best to mark as duplicate (and I put 4 references), because there are many other questions on the subject here. As I do not know exactly the origin of the divergence in your specific case, it becomes complicated to answer.

Show 4 more comments
No answers

Browser other questions tagged

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