Mortgage calculator

Thought I would share this here also. @Adam9812 @stevon8ter Here is the program that I pointed you to that you didn’t have permission to view. I originally had it in a discussion for beta testers for possible inclusion in examples for a new version of Codea.

-- mortgage calculator


function setup()
    boxTab={} -- table of input/output boxes
    -- x,y,w,h,type,text
    table.insert(boxTab,input(100,500,120,30,1,"Loan $ amount"))  
    table.insert(boxTab,input(400,500,120,30,1,"Yearly %  ( 5%=5 )"))  
    table.insert(boxTab,input(700,500,120,30,1,"Number of years")) 
    table.insert(boxTab,input(400,400,120,30,2,"Monthly payment"))  
    table.insert(boxTab,input(700,400,120,30,2,"Total payments"))  

function draw()
    background(34, 74, 82, 255) -- screen color
    fill(255,0,0)   -- set color to red
    text("MORTGAGE CALCULATOR",WIDTH/2,HEIGHT-50)   -- draw text
    fill(224, 149, 127, 255)    -- set color
    text("( Tap each box to input values )",WIDTH/2,HEIGHT-100)    -- draw text
    for a,b in pairs(boxTab) do -- loop thru table  
        b:draw()    -- draw each box
    amt=tonumber(boxTab[1].val)     -- starting amount
    int=tonumber(boxTab[2].val/100) -- yearly interest
    year=tonumber(boxTab[3].val)    -- number of years
    -- perform interest calculation
    i=int/12    -- interest per month
    n=year*12   -- total months
    m=amt*i*w/(w-1) -- calculate monthly amount
    boxTab[4].val=string.format("%.2f",m)   -- save monthly payment
    boxTab[5].val=string.format("%.2f",m*n) -- save total payments

function touched(t)    -- check which box is selected
    for nbr,box in pairs(boxTab) do -- loop thru table
        if box:touched(t) then  -- box touched
            return  -- dont check other boxes, exit function

function keyboard(k)
    for nbr,box in pairs(boxTab) do -- loop thru table
        box:keyboard(k) -- get input from box


function input:init(x,y,w,h,type,txt)
    self.x=x    -- x position
    self.y=y    -- y position
    self.w=w    -- width
    self.h=h    -- height
    self.type=type  -- 1=input box  2=output box
    self.txt=txt    -- text to show above box
    self.val="0"    -- data keyed in box
    self.sel=false  -- box selected true/false

function input:draw()
    fill(0)    -- set background for box
    if self.sel then    -- set selected color for box
        fill(105, 101, 31, 255)
    stroke(255, 66, 0, 255)       -- box outline color
    strokeWidth(2)    -- outline size
    rect(self.x,self.y,self.w,self.h)      -- draw box
    fill(255)         -- set text color
    text(self.txt,self.x,self.y+self.h)    -- box name
    text(self.val,self.x+4,self.y+4)       -- box text

function input:keyboard(k)
    if self.sel then
        if k==BACKSPACE then    -- backspace key pressed
            str=str:sub(1,str:len()-1)  -- remove last digit
            if str=="" then -- blank
                str="0" -- make 0
        elseif k>="0" and k<="9" or k=="." then   -- only digits 1 thru 9
            if str:sub(1,1)=="0" then   -- check if leading 0
                str=""  -- clear leading 0
            str=str..k  -- update keyed value
        self.val=str    -- save keyed value

function input:touched(t)
    if t.state==BEGAN then
        if not isKeyboardShowing() then
           showKeyboard()   -- show keyboard if its not displayed
        str="0" -- clear value
        for z=1,#boxTab do    -- clear selected flag for all boxes
        -- check which box was selected
        if t.x>self.x and t.x<self.x+self.w and
                 t.y>self.y and t.y<self.y+self.h and self.type==1 then
            self.val="0"      -- reset val
            self.sel=true     -- set selected flag
            return true       -- a box was selected
    return false    -- no box was selected

nice job @dave1707 i like it :slight_smile:

Nice. I do hope it gets included. :slight_smile:

@dave1707 - sorry to be pedantic, but compound interest is my field. While it is common to show total payments, I think it is very misleading, especially if used for comparison. You can’t add up all the payments made over many years and compare them to anything else, because 1,000 now is not worth the same as 1,000 in 20 years. So the total is completely meaningless.

@Ignatz I wouldn’t say it’s meaningless. It shows how much interest you would be paying if you made all the payments. It gives you an idea of how much more you pay for borrowing money.

@dave1707 - but if you add the 1,000 you pay this month to the 1,000 you pay in 25 years, you get 2,000. In reality, the true value is about 1,400.

(Put another way, if you still believe it is fair to add them, how about you lend me 1,000 and I will pay you back 1,000 in 25 years time?)

@Ignatz I think I know what you mean. Put another way, if you borrow 1,000 oz. of gold from me at 5% to be payed back over 6 years, you would owe me 16.1 oz. per month for 72 months. The total of those payments would be 1,159.56 oz. of gold. The price of gold will vary over those 6 years, and the total $ value isn’t known right now, but you would still owe me 1,159.56 oz, which is what the total payment shows. The same goes for $. The value of the $ may vary. You’ll make more per hour as you get raises, but you would still owe that total $ amount.

@dave1707 - exactly. Time has value, so you should never add amounts from different times together. You need to get them to the same point in time first. If that time is in the future, you add interest from the time the payment is paid, up to the time you are adding everything together. If the point in time is before the time a payment is made, you remove interest.

The simplest way to see it is that

Future value = Current value x (1+i)^n
Where n is number of years in the future

You can calculate current value from a future value using the same equation.