Search code examples
rubyexcel-formulaamortization

'+' String can't be coerced into Fixnum (TypeError)


this is a simple calculation formula im doing trying ruby for the first time and stuck on a simple error bt need some guidance

#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

Error when i calculate the formula I end up getting this [undefined method rate for main:Object (NoMethodError)]

monthly_payment = loan_amount((rate(1 + rate)**period)/(1 + rate)**period - 1)

puts "#{monthly_payment}"

Solution

  • There is a gem called Exonio: https://github.com/Noverde/exonio . This gem implements some of the Excel financials methods like: PMT, IPMT, PV, NPER...