Skip to content

Morgage Calculator

It’s late and this is a hack but it ~seems~ to be in the ballpark of what the figures were getting back from our loan officer. It’s not factoring in all the other stuff like closing and other taxes, but for a quick ref on how much you’ll be paying or would like to pay on a place it’s decent enough.

Since it was hard enough to dig up the right formula and such I’ll share my bad hack here and if enough people poke me about it it might end up on CPAN.

In short it allows for the following things to happen:

  • If I know how much I want to spend, what is the max loan I can cap out at?
  • benh:bin>  mcal --payment 1400 --tax 2100
    TOTAL LOAN: $241767.420
    
  • If I know the loan amount how much will I spend a month
  • benh:bin>  mcal --loan 232000 --tax 2100
    MONTHLY PAYMENT: $1350.510
    
  • You can ask both questions at once, you get to parse out the results
  • benh:bin>  mcal --loan 239000 --payment 1200 --tax 2100
    TOTAL LOAN: $202295.188
    MONTHLY PAYMENT: $1385.978
    

… now on with the code …

#!/usr/bin/perl
use strict;
use warnings;

my $mor = My::Mortgage>new_with_options();
$mor->calc;

BEGIN{
package My::Mortgage;
use Moose;
with qw{MooseX::Getopt};
has [qw{loan payment tax}] => (
   is => 'rw',
   isa => 'Num',
   default => -1,
);

has rate => (qw{is rw isa Num default 4.5});
has years => (qw{is rw isa Int default 30});

has c => (
   is => 'rw',
   isa => 'Num',
   lazy => 1,
   default => sub{
      my $s = shift;
      my $c = $s->rate/100/12;
      my $n = $s->years*12;
      return ((1+$c)**$n * $c)/((1+$c)**$n -1);
   },
);

sub calc_payment {
   my $self = shift;
   return ($self->tax >= 0 )
        ? ($self->loan * $self->c) + ($self->tax / 12)
        : $self->loan * $self->c
        ;
}

sub calc_loan {
   my $self = shift;
   return ($self->tax >= 0)
        ? ($self->payment - ($self->tax / 12) )/$self->c
        : $self->payment/$self->c
        ;
}

sub calc {
   my $self = shift;
   die 'NOT ENOUGH INFO GIVEN'
      unless $self->payment >= 0 || $self->loan >= 0;
   printf qq{TOTAL LOAN: \$%0.03f\n}, $self->calc_loan
      if $self->payment >= 0 ;
   printf qq{MONTHLY PAYMENT: \$%0.03f\n}, $self->calc_payment
      if $self->loan >= 0 ;
}

1;
};#END BEGIN

Like everything else posted in the wild, take and play, have fun and break it, just don’t come crying to me if it eats your puppy.

One Trackback/Pingback

  1. Morgage Calculator | Business News on Thursday, June 18, 2009 at 12:37

    [...] Read the rest of this great post here [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*