';
//print_r($postarray);
//echo '';
if(strstr($APR,'%')) { // strip %
$APR = strrev($APR);
$APR = substr($APR,strpos($APR,'%')+1,strlen($APR));
$APR = strrev($APR);
}
$APR = $APR*1000;
$rateblurb = 2;
if($APR >= 20001) {
$rateblurb = 3;
}
if($APR < 1000) {
$rateblurb = 1;
//if($APR < 1000) {
//$rateblurb = 1;
//}
}
if($APR == 0) {
$rateblurb = 0;
}
switch ($rateblurb){
case 0:
$warning = "Please provide a value.";
break;
case 1:
$APR = $APR*100;
$warning = "Did you mean ".$APR."%?";
break;
case 2:
$warning = '';
break;
case 3:
$foo = $APR/1000;
$warning = 'Rate '.$foo.'% is out of range.';
$APR = 0;
break;
default:
$warning = "Rate ERROR.";
break;
}
$APR = $APR/1000;
//echo $APR.'
';
$APR = $APR/100;
//echo $APR.'
';
$Rate = $APR/12;
$Nper = $yr*12;
$Pv = $postarray['Pv']; // get purchase value
if(strstr($Pv,'.')) {
$Pv = strrev($Pv);
$Pv = strrev(substr($Pv,strpos($Pv, '.')+1,strlen($Pv)));
}
if(strstr($Pv,'$')) {
$Pv = substr($Pv,strpos($Pv, '$')+1,strlen($Pv));
}
$Pva = array();
$Pva = split(',',$Pv); // split string using ","
$cnt = count($Pva); // how many pieces
$Pv = ""; // clear the purchas value
foreach($Pva as $key => $elem) {
$Pv .= $elem; // rebuild the purchase value
}
// hair that adds commas into number string
$Pvrev = strrev($Pv); // put LSD to left
if(strlen($Pvrev) > 4) { // if there are more than 4 digits add commas
addcomma($Pvrev); // add commas
} else {
$tmp = $Pvrev;
}
function addcomma($Pvrev) {
global $tmp;
$sl = strlen($Pvrev); // get the string length
if($sl >= 3) { // if 3 or more digits more to process
if($sl < 4) { // if 3 digits just concatenate
$tmp .= substr($Pvrev,0,3);
} else { // there are 4 or more, add a comma and go again
$tmp .= substr($Pvrev,0,3).',';
addcomma(substr($Pvrev,3,strlen($Pvrev))); // throw away LSD, LS1, & LS2
}
} else {
$tmp .= $Pvrev; // all done, concatenate
}
}
$Pvf = strrev($tmp); // flip the LSD to the right
$p = pow((1+$Rate),$Nper);
//echo $p.'
';
if($p == 1) {
$payment = 0;
} else {
$payment =sprintf("$%01.2f", ($Pv*$p*$Rate)/($p - 1));
}
$io =sprintf("$%01.2f", ($Pv*$Rate));
//echo $Pvf.'
';
//echo $APR.'
';
//echo $warning.'
';
//echo $yr.'
';
//echo $payment.'
';
//echo $io.'
';
?>