0

I am trying to do a simple condition where if the total of my previous transaction + the amount I want to charge on the credit is smaller or equal to the total amount invoice than I can charge the amount on the credit card

$amount_to_charge = (float)$_REQUEST['cc_amount'];
Log::error("PAID TYPE : ".gettype(($book->totalPaid() + $amount_to_charge)));
Log::error("Total Type : ".gettype($book->bookedGrandTotal()));
Log::error(($book->totalPaid() + $amount_to_charge)." <= ".$book->bookedGrandTotal());
if(($book->totalPaid() + $amount_to_charge) <= $book->bookedGrandTotal())
{
    Log::error("AMT OK");
}
else
{
    Log::error("AMT NOT OK");
    $error['ERROR'] = "The maximal amount you can charge is : $".($book->bookedGrandTotal()-$book->totalPaid());
}

Log response :

[2018-08-02 10:01:24] dev.ERROR: PAID TYPE : double
[2018-08-02 10:01:24] dev.ERROR: Total Type : double
[2018-08-02 10:01:24] dev.ERROR: 142.56 <= 142.56
[2018-08-02 10:01:24] dev.ERROR: AMT NOT OK

142.56 is supposed to be equal to 142.56 so I should enter in my if statement?

seb
  • 313
  • 1
  • 7
  • 19
  • 2
    It's due to the floating point percision, see: http://www.php.net/manual/en/language.types.float.php – maio290 Aug 02 '18 at 14:16
  • 1
    Try adding `round()` with 2 decimal places to each amount in the `if` like so `if(round(($book->totalPaid() + $amount_to_charge), 2) <= round($book->bookedGrandTotal(), 2))` – superphonic Aug 02 '18 at 14:20
  • Convert them to strings maybe. `strval(142.56) === strval(142.56)`. – nice_dev Aug 02 '18 at 14:25

0 Answers0