Category: Miscellaneus
Author/Contact Info contact me: /msg sulfericacid
Description: Based on principle, interest and duration this finds your monthly payments. Along with the answer the script shows you the actual equation and walks you through the steps of solving the problem.

To my knowledge this is accurate (my smarter older brother gave me the equation) but if you find this to be inaccurate please give me the proper equation so I can fix this!

UPDATE: A rough CGI version was written and can be found below.

Command Line Version
-----------------------

#!/usr/bin/perl -w

use strict;
use warnings;
my ( $totalinterest, $totaldue, $monthlypayment, $round );

print "Principle:";
chomp( my $principle = <STDIN> );

while ( $principle !~ /^-?\.?\d+(?:\.\d+)?$/ ) {
    print "Number you supplied did not look like a real number.\n";
    print "Only numbers and decimals are allowed, no commas!\n\n";
    print "Principle:";
    chomp( $principle = <STDIN> );
}

print "Interest (as a decimal):";
chomp( my $interest = <STDIN> );

while ( $interest !~ /^-?\.?\d+(?:\.\d+)?$/ ) {
    print "Number you supplied did not look like a real number.\n";
    print "Only numbers and decimals are allowed, no commas!\n\n";
    print "Interest (as a decimal):";
    chomp( $interest = <STDIN> );
}

print "Duation (in months):";
chomp( my $duration = <STDIN> );

while ( $duration !~ /^-?\.?\d+(?:\.\d+)?$/ ) {
    print "Number you supplied did not look like a real number.\n";
    print "Only numbers and decimals are allowed, no commas!\n\n";
    print "Interest (as a decimal):";
    chomp( $duration = <STDIN> );
}

print "\n---------------------------\n";

$totalinterest = $principle * $interest;
print "Total interest:   \$$totalinterest\n";
$totaldue = $totalinterest + $principle;
print "Total due:        \$$totaldue\n";
$monthlypayment = $totaldue / $duration;

$round = sprintf "%.2f", $monthlypayment;
print "Monthly price:    \$$round\n";
print "---------------------------\n\n";

print "How we did it:\n";
print "Actual equation: (principle * interest + principle) / months\n"
+;
print "Your equation:   ($principle \* $interest \+ $principle) \/ mon
+ths\n";
print "Step one:        ($totalinterest \+ $principle) \/ months\n";
print "Step two:        ($totaldue) \/ months\n";
print "Step three:            $round";
CGI Version
---------------------

#!/usr/bin/perl -w

use strict;
use warnings;
my ( $totalinterest, $totaldue, $monthlypayment, $round );

use CGI qw/:standard/;
print header, start_html(), start_form, "Principle (amount due): ",
  textfield('principle'), p, "Interest (as a decimal): ", textfield('i
+nterest'),
  p, "Duration (in months): ", textfield('duration'), p, submit, end_f
+orm, hr;

chomp( my $principle = param('principle') );
chomp( my $interest  = param('interest') );
chomp( my $duration  = param('duration') );

if ( $principle && $interest && $duration ) {

    #   if (param('principle') && param('interest') && param('duration
+')) {
    print "\n---------------------------<br>";

    $totalinterest = $principle * $interest;
    print "Total interest:   \$$totalinterest<br>";
    $totaldue = $totalinterest + $principle;
    print "Total due:        \$$totaldue<br>";
    $monthlypayment = $totaldue / $duration;

    $round = sprintf "%.2f", $monthlypayment;
    print "Monthly price:    \$$round<br>";
    print "---------------------------<br><br>";

    print "How we did it:<br>";
    print "Actual equation: (principle * interest + principle) / month
+s<br>";
    print "Your equation:   ($principle \* $interest \+ $principle) \/
+ $duration<br>";
    print "Step one:        ($totalinterest \+ $principle) \/ $duratio
+n<br>";
    print "Step two:        ($totaldue) \/ $duration<br>";
    print "Step three:            \$$round";
    hr;
}
else {
    print "Something was missing!<br>";
}

print end_html();
Replies are listed 'Best First'.
Re: Monthly Payment Analyzer (formulas)
by tye (Sage) on Apr 01, 2003 at 18:51 UTC

    See Payments calculator for more accurate formulas including how I derived them and a complete script that makes use of them (including checking the results).

                    - tye
      I took a look at your code and was overwhelmed at not being able to understand most of it. Honestly that is the most difficult code to follow as I've ever come across, no offense cause I still don't really understand all that much the way it is. Like for example:
      L= sum<j=0..N-1>( r^j ) L= 1+r+..+r^(N-1) r*L - L = r*( 1+r+..+r^(N-2)+r^(N-1) ) - ( 1+r+..+r^(N-1) ) r*L - L = ( r+r^2+..+r^(N-1)+r^N ) - ( 1+r+..+r^(N-1) ) r*L - L = r+r^2+..+r^(N-1) + r^N - ( 1 + r+..+r^(N-1) ) r*L - L = r+r^2+..+r^(N-1) + r^N - 1 - ( r+..+r^(N-1) )
      Holy cow, it looks like I opened a binary file in notepad :) You're saying your script is more accurate than mine but is my equation and everything suffient anyways? This wasn't meant to be dead accurate, just to help my girlfriend with her homework.

      Thanks!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        That isn't code. Those are comments showing how the relatively simple equation was derived. Namely:

        p = P * r^N / [ (r^N-1) / (r-1) ]
        where
        r = (1+i/f)

        I didn't look at your code in detail. Looking more closely now, I would say that either your concept of "interest" incorporates much more than the interest rate that would normally be quoted when getting a loan or your code is simplistic to the point of being very inaccurate.

        Try running my script with some sample data and compare it to your script if you want to get a better idea of how much difference there is.

        Note that my script wants the simple interest rate for the loan. This is not "APR" (Annual Percentage Rate) which takes into account how the simple interest is compounded.

        So a simple 8% loan with interest compounded monthly would have an APR of 100*( (1+0.08/12)^12 - 1 )% or about 8.3%. For such a loan, you'd tell my script "8%".

        Note that ^ is used for exponentiation in the math equations I've shown and you'd use ** instead if writing similar code in Perl.

        A somewhat simpler equation that I've seen loan officers use makes the assumption that you'll pay about 1/2 as much interest in paying off the loan as would accrue if you made no payments. Or:

        p = P * [ (1+i/f)^N + 1 ] / 2
        This equation is much simpler to derive, but is only a little simpler to use than my very accurate one, so I don't use it. I'm not sure why loan officers use it, since I doubt they care whether or not they know how to derive the formula. Perhaps it is easier to use with a typical adding machine?

                        - tye

        That's pseudo code in the documentation. It's what tye described as the derivation of his formulae.

        --
        Clayton aka "Tex"