# A new Simple Interest Calculator

Hello fellow Codea Coders! I’m back with a basic program. A simple interest calculator. It displays both the interest added and the total. While I probably will rarely use it, I mainly created it to expand my ability of making equations on Lua.

``````--This was made by Adam9812. It is public domain.

--Simple Interest Calculator 1.1

function setup()
print("Principle: Starting Number")
print("Rate: Rate interest is added or subtrancted a time unit.")
print("Time: Time passed.")
print("Interest: Amount of added interest")

interest = 0
parameter.number("principle",.01,10000,1)
parameter.number("rate",.01,1,.5)
parameter.number("time",1,100,5)

parameter.watch("interest")

end

function draw()
background(192, 192, 209, 255)

interest = principle*(rate)*time

fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)

text(principle + interest,WIDTH/2,HEIGHT - 90)
end
``````

@Adam9812 - the code looks ok to me, good work.

@Adam9812 Nice code. Some suggestions if you want to make updates to this. A better description of your Time. What’s the amount of time you’re talking about. Another is the output display. Codea version 2 now uses 64 bit calculations, so it can display as many as 15 digits to the right of the decimal point. You might want to look into using string.format to format your output to limit it to 2 digits on the right side of the decimal point.

The time is whatever unit you use. It works as long as you use the same unit with all the parameters. For example, if you use semiannual for the rate (by using the rate as how much interest added semiannually) then use the time for semiannual.

@Adam9812 - I know it’s only a way of practising programming, but I thought the same as @dave1707. It’s very easy to get confused about time periods and interest rates, which is why I would always work with annual rates and time periods which are fractions of years (i used to work a lot with interest rates in forecasting models).

Also, if you want more practice, try doing compound interest!

I am looking into doing a compound interest calculator. I will probably do it this weekend.

Never mind, it was much easier than previously thought. I hard capped the compound rate at 12 because it seems no one would have a higher one.

``````--This was made by Adam9812. It is public domain.

--Compound Interest Calculator 0.4

function setup()
print("Principle: Starting Number")
print("Rate: Rate interest is added or subtracted per year.")
print("Time: Time passed in Years.")
print("Compoundrate: The amount of time interest is compounded per year.")
print("Interest: Amount of added interest")

interest = 0
parameter.number("principle",.01,10000,1)
parameter.number("rate",.01,1,.5)
parameter.number("time",1,100,5)
parameter.number("compoundrate",1,12,1)

parameter.watch("interest")

end

function draw()
background(192, 192, 209, 255)

interest = principle * (1 + rate/compoundrate) * compoundrate * time

fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)

text(principle + interest,WIDTH/2,HEIGHT - 90)
end
``````

@Adam9812 - perhaps a little too easy! Try this instead

``````interest = principle * ((1 + rate/compoundrate) ^ (compoundrate* time) - 1)
``````

Are you sure? I got the equasion from this site. http://math2.org/math/general/interest.htm

@Adam9812 - the formula at that site is correct, but notice that the letters rt are written as a small superscript, which means they are not multiplied, but are powers.

Try calculating a couple of simple examples and you’ll soon see which looks right!

Ok thanks!

The updated Code

``````--This was made by Adam9812. It is public domain.

--Compound Interest Calculator 0.4

function setup()
print("Principle: Starting Number")
print("Rate: Rate interest is added or subtracted per year.")
print("Time: Time passed in Years.")
print("Compoundrate: The amount of time interest is compounded per year.")
print("Interest: Amount of added interest")

interest = 0
parameter.number("principle",.01,10000,1)
parameter.number("rate",.01,1,.5)
parameter.number("time",1,100,5)
parameter.number("compoundrate",1,12,1)

parameter.watch("interest")

end

function draw()
background(192, 192, 209, 255)

interest = principle * (1 + rate/compoundrate) ^ compoundrate * time

fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)

text(principle + interest,WIDTH/2,HEIGHT - 90)
end
``````

@Adam9812 - not quite there yet

Use the formula I gave you above

Now it seems to go up at a exponential rate. It is nmuch more compound-y

``````--This was made by Adam9812. It is public domain.

--Compound Interest Calculator 0.4

function setup()
print("Principle: Starting Number")
print("Rate: Rate interest is added or subtracted per year.")
print("Time: Time passed in Years.")
print("Compoundrate: The amount of time interest is compounded per year.")
print("Interest: Amount of added interest")

interest = 0
parameter.number("principle",.01,10000,1)
parameter.number("rate",.01,1,.5)
parameter.number("time",1,100,5)
parameter.number("compoundrate",1,12,1)

parameter.watch("interest")

end

function draw()
background(192, 192, 209, 255)

interest = principle * ((1 + rate/compoundrate) ^ (compoundrate* time) - 1)

fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)

text(principle + interest,WIDTH/2,HEIGHT - 90)
end
``````

@Adam9812 Nice job. Here’s a line of code you might want to add. It limits the answer to 2 decimal places.

``````
interest = principle * ((1 + rate/compoundrate) ^ (compoundrate* time) - 1)

interest=string.format("%.2f",interest)       -- add this line after the line above

``````

Here is a version that lets you select whether you do simple or compound. And thanks Dave, the line was added!

``````--This was made by Adam9812. It is public domain.

--Interest Calculator 1.9

function setup()
print("Principle: Starting Number")
print("Rate: Rate interest is added or subtracted per year.")
print("Time: Time passed in Years.")
print("Simple: Whether simple or compound interest is calculated.")
print("Compoundrate: The amount of time interest is compounded per year.(Only matters if simple is false)")
print("Interest: Amount of added interest")

interest = 0
parameter.number("principle",.01,10000,1)
parameter.number("rate",.01,1,.5)
parameter.number("time",1,100,5)
parameter.boolean("simple",true)
parameter.number("compoundrate",1,12,1)

parameter.watch("interest")

end

function draw()
background(192, 192, 209, 255)

if simple then

interest = principle * rate * time

else

interest = principle * ((1 + rate/compoundrate) ^ (compoundrate* time) - 1)

end

interest=string.format("%.2f",interest)

fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)

text("Total: "..principle + interest,WIDTH/2,HEIGHT - 90)
end
``````

@Adam9812 That’s one way to improve you programming skills. Take something you’ve written and keep adding more options to it. Good job. I created a mortgage calculator shown at the link below that you might find interesting.

``````http://twolivesleft.com/Codea/Talk/discussion/4775/proposed-projects-for-inclusion-with-codea-2-0-as-learning-projects#Item_13
``````

Thanks! This community is among the best I’ve been in. Without it, I would still be poking around in the dark about how to program in lua.

@Adam9812 I just realized that the string.format you added wasn’t being used correctly. Try these changes in draw().

``````function draw()
background(192, 192, 209, 255)
if simple then
interest = principle * rate * time
else
interest = principle * ((1 + rate/compoundrate) ^ (compoundrate* time) - 1)
end
fill(0, 0, 0, 255)
font("AmericanTypewriter-Light")
fontSize(90)
str=string.format("Total: %.2f",principle+interest)
text(str,WIDTH/2,HEIGHT - 90)
end
``````

Ok, thanks!