红宝石贷款计算

时间:2016-09-05 11:45:16

标签: ruby

我有这些代码:

require 'yaml'
MESSAGES = YAML.load_file('mortgage_calc_msg.yml')

LANGUAGE = 'en'

def prompt(message)
  Kernel.puts(message)
end

def messages(message, lang='en')
  MESSAGES[lang][message]
end

def integer?(input)
  input.to_i.to_s == input
end

def float?(input)
  input.to_f.to_s == input
end

def valid_number?(input)
  integer?(input) || float?(input)
end

prompt(messages('welcome', LANGUAGE))

loop do # main loop
  loan_amt = ''
  loop do
    prompt(messages('loan_amt', LANGUAGE))
    loan_amt = Kernel.gets.chomp

    if valid_number?(loan_amt)
      break
    else
      prompt(messages('not_valid_num', LANGUAGE))
    end
  end

  loop do
    prompt(messages('APR_amt', LANGUAGE))
    APR_amt = Kernel.gets.chomp

    if valid_number?(APR_amt)
      break
    else
      prompt(messages('not_valid_num', LANGUAGE))
    end
  end

  loan_duration = ''
  loop do
    prompt(messages('loan_duration', LANGUAGE))
    loan_duration = Kernel.gets().chomp()

    if valid_number?(loan_duration)
      break
    else
      prompt(messages('not_valid_num', LANGUAGE))
    end
  end

  annual_interest_rate = APR_amt.to_f() / 100
  monthly_interest_rate = annual_interest_rate / 12
  months = loan_duration.to_i() * 12

  monthly_payment = loan_amt.to_f() *
                    (monthly_interest_rate /
                    (1 - (1 + monthly_interest_rate)**-months.to_i()))

  prompt("Your monthly payment is: $#{format('%02.2f', monthly_payment)}")
  prompt(messages('reset', LANGUAGE))
  answer = Kernel.gets().chomp()
  break unless answer.downcase() == 'Y'
end

prompt(messages('goodbye_msg', LANGUAGE))

基本上它的作用是计算:

一个月的利率 - 月的持续时间

我知道这是红宝石代码。

但是,那里的任何人都可以在这里讨论这行代码如何通过虚拟方式或外行人的术语进行处理吗?

annual_interest_rate = APR_amt.to_f() / 100
  monthly_interest_rate = annual_interest_rate / 12
  months = loan_duration.to_i() * 12

  monthly_payment = loan_amt.to_f() *
                    (monthly_interest_rate /
                    (1 - (1 + monthly_interest_rate)**-months.to_i()))

  prompt("Your monthly payment is: $#{format('%02.2f', monthly_payment)}")

我为成为一名新手而道歉。

1 个答案:

答案 0 :(得分:0)

annual_interest_rate = APR_amt.to_f() / 100

这计算年度金额并转换为浮动,然后除以100 ..