![]() |
Salt Lake City Thursday, 2008.11.20 17:20 MDT [GMT-7] |
| Home - IT: IT Security - Programming - OS - HW - SW - Internet - IT News - Technology - Science - Communication - News: World - USA - USA States - Alternative - Business - Investment - more coming soon... | |
# show loan interest
# version 2: multiple payments
#
$i = 0;
$payIndex = 0;
# @PAYMENT[0] = 0; # ? is it really necessary??
while ( $i < $#ARGV )
{
if ( @ARGV[$i] eq "-r" )
{
$RATE=@ARGV[++$i];
}
else
{
if ( @ARGV[$i] eq "-a" )
{
$AMOUNT=@ARGV[++$i];
}
else
{
if ( @ARGV[$i] eq "-p" )
{
@PAYMENT[$payIndex++]=@ARGV[++$i];
}
else
{
print "Unknown argument (@ARGV[$i])\n";
}
}
}
$i++;
}
if ( $AMOUNT == 0 || $RATE == 0 || @PAYMENT[0] == 0 )
{
print "Specify -r rate -a amount -p payment\n";
exit;
}
# echo clear;
print "Original Balance: \$$AMOUNT\n";
print "Interest Rate : ${RATE}%\n";
print "Monthly Payment : \$@PAYMENT\n";
print "\n";
print "Month\tPayment\tInterest\tPrincipal\tBalance\n\n";
$month=1;
$rate=$RATE/12/100;
$balance=$AMOUNT;
# $payment=$PAYMENT;
$loop = 0;
while ($balance > 0 )
{
if ( $loop < $payIndex )
{
$payment = @PAYMENT[$loop];
++$loop;
}
$interest = roundUpAmount($rate * $balance);
$principal = roundUpAmount($payment - $interest);
if ( $balance < $principal )
{
$principal=$balance;
$payment=$principal + $interest;
}
$balance=roundUpAmount($balance - $principal);
print "$month\t\$$payment\t\$$interest\t\t\$$principal\t\t$\$balance\n";
$month++;
}
sub roundUpAmount
{
$value=$_[0];
$newvalue=( int ( ( $value * 100 ) + .5 ) ) / 100;
return ($newvalue);
}
Vincenzo Maggio