将公式转换为 JavaScript
Converting formula to JavaScript
我正在尝试构建一个采用三个参数的 "How Much car can I afford" 计算器:月供、利率和贷款期限。然后根据用户输入的数据计算这三个值,以向用户展示他们可以负担得起的汽车价值。然后我得到了以下公式:
CarValue = (MONTHLY PAYMENT * (1-(1+(INTEREST RATE / 12)) ^ -TERM)) / (RATE / 12)
使用 ECMAScript 5 我正在尝试将此公式转换为 JavaScript 并且它给了我疯狂的结果:
var monthlyPayment = $scope.model.howMuchCanIAfford.monthlyPayment;
var interestRate = $scope.model.howMuchCanIAfford.interestRate;
var loanLength = $scope.model.howMuchCanIAfford.loanLength;
var monthlyAmount = (monthlyPayment * (Math.pow(1-((1+(interestRate / 12))), (-1 * loanLength)))) / (interestRate / 12);
示例数据:
monthlyPayment = 700
interestRate = 9.2
loanLength = 84
output: ,503,380,362,412.83
显然输出量有很大偏差,我不确定是我的公式转换出了问题,还是我得到的原始公式有误。
应该是:
var monthlyAmount = (monthlyPayment * ( 1- Math.pow( (1+(interestRate / 12)), (-1 * loanLength) ))) / (interestRate / 12);
在原来的公式中,-1 位于括号外,是次幂。
我正在尝试构建一个采用三个参数的 "How Much car can I afford" 计算器:月供、利率和贷款期限。然后根据用户输入的数据计算这三个值,以向用户展示他们可以负担得起的汽车价值。然后我得到了以下公式:
CarValue = (MONTHLY PAYMENT * (1-(1+(INTEREST RATE / 12)) ^ -TERM)) / (RATE / 12)
使用 ECMAScript 5 我正在尝试将此公式转换为 JavaScript 并且它给了我疯狂的结果:
var monthlyPayment = $scope.model.howMuchCanIAfford.monthlyPayment;
var interestRate = $scope.model.howMuchCanIAfford.interestRate;
var loanLength = $scope.model.howMuchCanIAfford.loanLength;
var monthlyAmount = (monthlyPayment * (Math.pow(1-((1+(interestRate / 12))), (-1 * loanLength)))) / (interestRate / 12);
示例数据:
monthlyPayment = 700
interestRate = 9.2
loanLength = 84
output: ,503,380,362,412.83
显然输出量有很大偏差,我不确定是我的公式转换出了问题,还是我得到的原始公式有误。
应该是:
var monthlyAmount = (monthlyPayment * ( 1- Math.pow( (1+(interestRate / 12)), (-1 * loanLength) ))) / (interestRate / 12);
在原来的公式中,-1 位于括号外,是次幂。