1

I´ve a strange problem and I hope you can help me.

I´ve two values at the beginning:

var_dump($order_sum);
var_dump($orders_obj->getOrders_preisliste_sum ());
var_dump($order_sum != $orders_obj->getOrders_preisliste_sum ());

Result:

float(49.7) 
string(7) "49.7000" 
bool(true)

Here all is okay, but why is that way also "true" if I compare them?

var_dump($order_sum);
var_dump(floatval($orders_obj->getOrders_preisliste_sum ()));
var_dump($order_sum != floatval($orders_obj->getOrders_preisliste_sum ()));

Result:

float(49.7) 
float(49.7) 
bool(true) <---

In my opinion it should be the same, but here PHP displays it as a different.

Thomas K.
  • 21
  • 1
  • [Read some of this until you get the gist of what they are saying](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). Floating point precision is more complex than it first appears. [This answer](http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values) might also provide some insight. – Adam Apr 10 '14 at 20:45
  • @Adam: They should both be `49.700000000000003` – AbraCadaver Apr 10 '14 at 20:50
  • I didn't think anything of it. Thanks for help. I´ve rounded the values and now all is correct. – Thomas K. Apr 12 '14 at 09:45

1 Answers1

2

You could use bccomp function (bcmath extension) to compare your float values : http://www.php.net/manual/en/function.bccomp.php. Your values are actually not equals because of floating point precision as Adam just mentioned.