0

I am trying to compare 2 values in PHP.

My logic is:

  1. I have a remaining amount (a)
  2. I have a amount to be charged (b)
  3. I calculate remaining to be by ( a - b )
  4. After charge action I get the actual remaining value (c)
  5. I compare the value I got in #3 with (c)

Even though the both are similar PHP says they are not equal.

Below given is my code (with filled values)

<?php
$remaining_amount_before_payment = "600";
$remaining_amount_after_payment = (float)$remaining_amount_before_payment - (float)"387.60";
$actual_remaining_amount_after_payment = "212.4";
echo "actual_remaining_amount_after_payment: {$actual_remaining_amount_after_payment} <br><br>";
echo "remaining_amount_after_payment: {$remaining_amount_after_payment} <br><br>";
var_dump( ((float)$actual_remaining_amount_after_payment) == ((float)$remaining_amount_after_payment) );?>

I type cast the values to float, but the var_dump returns FALSE.

Can anybody help me to find out why this is?

I am using PHP 5.6.

Thanks in advance!

masterFly
  • 1,072
  • 12
  • 24
  • 1
    a) why do you write your numbers as strings in the first place? there is absolutly **no** reason ever to do that. b) you do know that floating point arithmetics are *not* precise? you know and have read the [floating point guide](http://floating-point-gui.de/)? c) and as you said yourself: "Even though the both are similar PHP says they are not equal." - you know that "similar" and "equal" are not the same? ;) – Franz Gleichmann Feb 07 '17 at 08:31
  • 1
    Because the actual values I am getting is from a API and they come as strings! That's why I cast them to `float` since I have no control over the API response! I did not know about the point (b) you said. Is it not good to use `float` to type cast? – masterFly Feb 07 '17 at 08:33

3 Answers3

1

Bingo!

After several attempts I caught the catch. I was going crazy.

The "problem" is inside the right rounding values

$remaining_amount_before_payment = floatval("600"); // use floatval istead of (float)
$remaining_amount_after_payment = round($remaining_amount_before_payment - floatval("387.60"), 2);// use floatval istead of (float) and round result
$actual_remaining_amount_after_payment = floatval("212.4");// use floatval
echo "actual_remaining_amount_after_payment: {$actual_remaining_amount_after_payment} <br><br>";
echo "remaining_amount_after_payment: {$remaining_amount_after_payment} <br><br>";

var_dump( $actual_remaining_amount_after_payment === $remaining_amount_after_payment ); // return TRUE

Example

Voilà!

Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
0

Use var_dump(abs(floatval($actual_remaining_amount_after_payment) == floatval($remaining_amount_after_payment)) == 0);

Sakezzz
  • 468
  • 6
  • 16
0

acual your variable '$remaining_amount_after_payment' is not realy 212.4

use a var_export to determine its value. In my concern, You should "round" your floats values to a precision. round(x, precision) for comparison

okante
  • 151
  • 6