'+' 字符串不能被强制转换为 Fixnum (TypeError)
'+' String can't be coerced into Fixnum (TypeError)
这是一个简单的计算公式,我是第一次尝试 ruby,遇到了一个简单的错误,需要一些指导
#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
计算公式时出错我最终得到这个[main:Object 的未定义方法率(NoMethodError)]
monthly_payment = loan_amount((rate(1 + rate)**period)/(1 + rate)**period - 1)
puts "#{monthly_payment}"
方法gets
总是return字符串
loan_amount = gets
=> "1"
您应该在计算之前将字符串转换为数字:
loan_amount = gets
loan_amount = loan_amount.to_f
...
有一个名为 Exonio 的 gem:https://github.com/Noverde/exonio。
此 gem 实现了一些 Excel 财务方法,例如:PMT、IPMT、PV、NPER...
这是一个简单的计算公式,我是第一次尝试 ruby,遇到了一个简单的错误,需要一些指导
#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
计算公式时出错我最终得到这个[main:Object 的未定义方法率(NoMethodError)]
monthly_payment = loan_amount((rate(1 + rate)**period)/(1 + rate)**period - 1)
puts "#{monthly_payment}"
方法gets
总是return字符串
loan_amount = gets
=> "1"
您应该在计算之前将字符串转换为数字:
loan_amount = gets
loan_amount = loan_amount.to_f
...
有一个名为 Exonio 的 gem:https://github.com/Noverde/exonio。 此 gem 实现了一些 Excel 财务方法,例如:PMT、IPMT、PV、NPER...