note 100443 deleted from function.number-format by cmb
| From: | cmb@php.net | Date: | Sun, 08 Sep 2019 10:15:01 +0000 |
| Subject: | note 100443 deleted from function.number-format by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212122@lists.php.net to get a copy of this message | ||
Note Submitter: Ricky *@* Mataka [com]
----
Hey Guys, dont know if this is the best way but it worked for me as i faced a problem where I had to
split any large number > then 10K into into to exact split amounts.. so i wrote this this to do
the trick.. hope it helps some one out there!
<?php
function divide22($num)
{
$valone = intval($num / 2);
$valtwo = $valone;
if (($num % 2) > 0) $valtwo++;
return array($valone, $valtwo);
}
function split2vals($num,$split_amount)
{
$store=array();
$times = round(($num/$split_amount));
$amount= $num/$times;
for($i=0;$i<$times;$i++)
{
$store[]=round($amount);
}
foreach($store as $index=>$value)
{
if($value>$split_amount)
{
$get_splits=divide22($value);
//unset the split
unset($store[$index]);
$store[]=$get_splits[0];
$store[]=$get_splits[1];
}
}
$sum=array_sum($store);
if($sum > $num){
$sub=$sum-$num;
$end=end($store);
array_pop($store);
$store[]=$end-$sub;
}
if($sum < $num){
$store[]=($num-$sum);
}
return $store;
}
//USAGE
$check_amount=50567.83;
echo 'Input Split = '.$check_amount.'<br><br>';
$store=split2vals($check_amount,10000);
print_r($store);
$sum=array_sum($store);
echo '<br><br>Should Be Exact ='. $sum;
exit();
?>