title: Comparing floats - same number, but does not equal?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison">How should I do floating point comparison?</a><br>
    <a href="https://stackoverflow.com/questions/5230305/php-integer-and-float-comparison-mismatch">php integer and float comparison mismatch</a>  </p>
  </blockquote>
  
  
  
  <p>I have two variables, <code>$_REQUEST['amount']</code> and <code>$carttotal</code>, on an e-commerce thing. They of course should match when attempting to process a payment, so as to prevent a manual override of the payment amount at the last minute, or of course, a calculation error. </p>
  
  <p>However:</p>
  
  <pre><code>$carttotal = $carttotal * 1;
  $_REQUEST['amount'] = $_REQUEST['amount'] * 1;
  
  if($carttotal != $_REQUEST['amount']) {
      $code = 0; // cart empty under this user - cannot process payment!!! 
      $message = 'The cart total of ' . $carttotal . ' does not match ' . $_REQUEST['amount'] . '. Cannot process payment.';
      $amount = $carttotal;
      $json = array('code' =&gt; $code,
                    'message' =&gt; $message,
                    'amount' =&gt; $amount);
      die(json_encode($json));
  } else {
      $trnOrderNumber = $client-&gt;id . '-' . $carttotal;
  }
  </code></pre>
  
  <p>The above code, with the same numbers passed, is NOT giving me the equal. Basically I get the error message as if the <code>$carttotal != $_REQUEST['amount']</code> is <code>true</code> (unequal vars). </p>
  
  <p>So to test the vars, I snuck in:</p>
  
  <pre><code>var_dump($_REQUEST['amount']);
  var_dump($carttotal);
  </code></pre>
  
  <p>To see what is going on (after I do the <code>* 1</code> calculations to make sure they are dealt with as floats, not strings). </p>
  
  <p>I got this back: </p>
  
  <pre><code>float(168.57)
  float(168.57)
  </code></pre>
  
  <p>Very very frustrating. What could be causing this? </p>