我正在一个客户的网站上工作,我正在写一个摊铺计划计算器在轨道上的红宝石。对于较长的贷款期限计算,它似乎并没有被打破,当余额达到0Ruby/Rails while循环不正确?
这里是我的代码:
def calculate_amortization_results
p = params[:price].to_i
i = params[:rate].to_d
l = params[:term].to_i
j = i/(12*100)
n = l * 12
m = p * (j/(1 - (1 + j) ** (-1 * n)))
@loanAmount = p
@rateAmount = i
@monthlyAmount = m
@amort = []
@interestAmount = 0
while p > 0
line = Hash.new
h = p*j
c = m-h
p = p-c
line["interest"] = h
line["principal"] = c
if p <= 0
line["balance"] = 0
else
line["balance"] = p
end
line["payment"] = h+c
@amort.push(line)
@interestAmount += h
end
end
这里是视图:
- @amort.each_with_index do |a, i|
%li
.m
= i+1
.i
= number_to_currency(a["interest"], :unit => "$")
.p
= number_to_currency(a["principal"], :unit => "$")
.pp
= number_to_currency(a["payment"], :unit => "$")
.b
= number_to_currency(a["balance"], :unit => "$")
我我看到的是,代替最终付款余额0.00美元,它显示“ - $ - inf”,迭代一个循环,然后显示$ 0.00,但显示“ - $ - inf”感兴趣。它应该循环,直到p变为0,然后停止并将余额设置为0,但不是。任何想法我做错了什么?
该计算器是here。对于较短的条件(如5年)似乎工作正常,但较长的条款导致上述错误。
编辑:
更改while循环n.times do
,然后改变平衡以
= number_to_currency(a["balance"], :unit => "$", :negative_format => "$0.00")
是一个解决办法,但我想知道为什么while循环止跌不能正常工作
to_d是BigDecimal,但使用浮动工作,谢谢。 – aperture
不错的看点:+1 :) – apneadiving
谢谢,apneadiving :) – Tilo