#!/usr/local/bin/perl
# canadian payemnts using table from Pat Pelletier
print "Content-type: text/html\n\n";
print "
APR/Front End Cost Calculator\n";
print "\n";
$args = $ENV{'QUERY_STRING'};
$args =~ s/\+/ /g;
$args =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
if (length($args) < 2)
{
print <How much does that loan REALLY Cost?
EmptyForm
}
else
{
print "";
@f = split("&",$args);
for ($i=0; $i<=$#f; $i++)
{
$av = @f[$i];
if ($av =~ /^amt/) { $amt = substr($av,4); }
if ($av =~ /^int/) { $int = substr($av,4); }
if ($av =~ /^clc/) { $clc = substr($av,4); }
if ($av =~ /^pts/) { $pts = substr($av,4); }
if ($av =~ /^trm/) { $trm = substr($av,4); }
}
printf("\$ %12.2f Loan
at %6.3f %% for %2d Years
\n",
$amt,$int,$trm);
$mi = $int/1200; $tt = $trm * 12; $m2 = $apr/1200;
$temp = (1.0 / (1 + $mi) ** $tt);
$temp2 = (1.0 / (1 + $m2) ** $tt);
if ($temp < 1) { $paymt = $amt * $mi / (1 - $temp); }
else { $paymt = $amt / $trm; }
printf("Principal & Interest Monthly Payment : \$ %9.2f
\n",$paymt);
$pta = $amt * $pts / 100; $costs = $pta + $clc;
printf("With %6.3f %% points (\$ %7.2f) and other costs of \$ %7.2f
\n",
$pts,$pta,$clc);
printf("Your total upfront costs are: \$ %12.2f
\n",$costs);
$eamt = $amt + $costs;
printf("So the effective amount of your loan is: \$ %12.2f
\n",$eamt);
$epaymt = $eamt * $mi / (1 - $temp);
printf("The Monthly Payment for this amount would be : \$ %9.2f
\n",$epaymt);
printf("But the loan amount is really \$ %12.2f, so
",$amt);
$m1 = $mi; $m2 = 2 * $mi;
for ($j=0; $j<25; $j++)
{
$mnew = ($m1 + $m2)/2;
$temp = (1.0 / (1 + $mnew) ** $tt);
$epaymt2 = $amt * $mnew / (1 - $temp);
if ($epaymt2 < $epaymt) { $m1 = $mnew; } else { $m2 = $mnew; }
#printf("%12.7f %12.7f\n",$m1,$m2);
}
$apr = ($m1 + $m2)*600;
printf("The APR is therefore %6.4f %%
\n",$apr);
}
print<