这是一个简单的计算公式,我第一次尝试ruby并坚持一个简单的错误bt需要一些指导
#input
loan_amount = 0
rate = 0
period = 0
#output
monthly_payment = 0
print "Enter loan_amount: "
loan_amount = gets.to_f
print "Enter rate: "
rate = gets.to_f
print "Enter period: "
period = gets.to_f
monthly_payment = loan_amount((rate(1 + rate)**period)/(1 + rate)**period - 1)
puts "#{monthly_payment}"
答案 0 :(得分:1)
方法gets
始终返回字符串
loan_amount = gets
=> "1"
您应该在计算之前将字符串转换为数字:
loan_amount = gets
loan_amount = loan_amount.to_f
...
答案 1 :(得分:0)
有一种名为Exonio的宝石:https://github.com/Noverde/exonio。 这个gem实现了一些Excel财务方法,如:PMT,IPMT,PV,NPER ......