Skip to content

Instantly share code, notes, and snippets.

@TheNicholasNick
Created May 30, 2015 09:17
Show Gist options
  • Save TheNicholasNick/1eab540c363ea621a95f to your computer and use it in GitHub Desktop.
Save TheNicholasNick/1eab540c363ea621a95f to your computer and use it in GitHub Desktop.
calcs
# p = Principle (rrp, amount financed, vehicle cost)
# i = interest rate (pass in as rate/100/12, so 8.56% = 8.56/100/12 = 0.00713, i = 0.00713
# t = term
# r = residual
# pw = 1 or 0, payment when, 1 = advance, 0 = arrears
def self.monthly_payment(p,i,t,r,pw)
#
# Monthly rate is computed as follows:
#
# p*i*(1+i)**t - i*r
# M = --------------------
# (1+i)*((1+i)**t - 1)
#
# or:
#
# M = A*p + B*r
#
# where:
#
# denom = (1+i)*((1+i)**t - 1)
# A = i*(1+i)**t / denom
# B = -i / denom
#
if pw == 1 # 0 for end of period and 1 for beginning of period
denom = (1+i)*((1+i)**t - 1)
else
denom = ((1+i)**t - 1)
end
a = i*(1+i)**t / denom
b = -i / denom
BigDecimal.new("#{a*p + b*r}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment