Created
March 31, 2014 20:31
-
-
Save ajvargo/9901552 to your computer and use it in GitHub Desktop.
Calculate monthly payments to be done in N months
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# This prints a table showing how much a monthly payment | |
# would need to be to pay off a loan. It takes a max and | |
# min loan amount, and how much to jump between them. I | |
# use it to dream about paying off student loans. It allows | |
# me to see what my 1 year pay-off payments are as the loan | |
# gets paid back. | |
class PaymentCalculator | |
def initialize args | |
monthly_interest = (args[:interest] > 1 ? args[:interest] / 100.0 : args[:interest]) / 12.0 | |
payment_exponent = -1.0 * args[:number_of_payments] | |
@payment_formula = proc{ |principal| principal * (monthly_interest / (1 - (1 + monthly_interest) ** payment_exponent)) } | |
@payment_enumerator = args[:max_principal].step(args[:min_principal], args[:step]) | |
end | |
def show_payments | |
@payment_enumerator.each do |principal| | |
puts "\t%.2f\t%.2f" % [principal, @payment_formula.call(principal)] | |
end | |
end | |
end | |
if __FILE__==$0 | |
params = { | |
max_principal: 100_000, # most to check | |
min_principal: 50_000, # least to check | |
interest: 4.0, # as 5.0 or .005, annual | |
number_of_payments: 60, # pay of in N months | |
step: -1000 # its setup to step from max to min by step | |
} | |
PaymentCalculator.new(params).show_payments | |
# $ ./loan_repayment.rb | |
# 100000.00 1841.65 | |
# 99000.00 1823.24 | |
# 98000.00 1804.82 | |
# 97000.00 1786.40 | |
# 96000.00 1767.99 | |
# 95000.00 1749.57 | |
# 94000.00 1731.15 | |
# 93000.00 1712.74 | |
# 92000.00 1694.32 | |
# 91000.00 1675.90 | |
# 90000.00 1657.49 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment