mardi 27 décembre 2016

php - Foreach loop trough array with decimal numbers to reduce tokens. codeigniter

Been twisting my head on a foreach loop that decreses tokens. Attaching code that runs. What i try is to run trough the orders with same price (4 in total here, but after 3 are processed, i can not make it break. Also total tokens/coins add up to 4000 and should go up to 5000 leaving last order with xxxx coins left. Also the < > in the if statements are acting funny, probarly due it's a 8 digit decimal to play with.

The break should be the $user_amout initially and update the last order with remaining tokens to database.

$user_price =  '0.00002450';
$user_amount = '5000.00000000';

  if ($check_price == $user_price) {
  // We found a match, buy the amount of tokens
  echo "<br />We found Price a match<br />";
  $equalorder = $this->getEqual($type, $user_price);
  var_dump($equalorder);
  foreach ($equalorder as $equal) {
    if ($user_amount > $equal->amount){
      $to_reduce = $equal->amount;
    }
    $user_amount = bcsub($user_amount,$to_reduce,8);
    $order_check += $user_amount;
    //$user_amount -= $to_reduce;
    echo "<br> Coins to process from this order : ";
    echo $user_amount ;
    echo "<br>";
    if ($order_check == '5000.00000000') {
      break;
    }
  }  // End foreach loop
  echo "<br /><br /> Total coins processed : ";
  echo $order_check;
}

the result i get when running it with the var dump included.

    We found Price a match
array(4) { [0]=> object(stdClass)#29 (3) { ["id"]=> string(1) "1" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "2500.00000000" } [1]=> object(stdClass)#28 (3) { ["id"]=> string(1) "2" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "1000.00000000" } [2]=> object(stdClass)#27 (3) { ["id"]=> string(1) "4" ["price"]=> string(10) "0.00002450" ["amount"]=> string(13) "4000.00000000" } [3]=> object(stdClass)#26 (3) { ["id"]=> string(1) "9" ["price"]=> string(10) "0.00002450" ["amount"]=> string(14) "10000.00000000" } } 
Coins to process from this order : 2500.00000000

Coins to process from this order : 1500.00000000

Coins to process from this order : 500.00000000

Coins to process from this order : -500.00000000


Total coins processed : 4000

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire