【发布时间】:2015-12-11 01:34:54
【问题描述】:
我正在尝试使用 javascript 计算复利。我相信我有我需要的所有价值观,我也有公式。我正在努力解决如何将公式的^ 部分转换为Math.pow()。很明显,我不知道如何在下面正确使用它......
公式如下:
A = P(1 + r/n)^nt
n = 365 – assuming daily compounding
P = Principal
r = interest rate
t = years
A = accrued amount: principal + interest
这是我目前所拥有的:
totalInterest = (principal) * (1 + loanInterestRate / 365)(Math.pow(daysOfInterest, yearsOfInterest));
例如,我将素数设置为3.25%,付款到期日为12/30/2016。使用值看起来像这样:
(50000) * (1 + 0.0325 / 386) Math.pow(386, 1);
// 386 is the number of days from today till 12/30/2016.
// 1 is: 1 year from today till 12/30/2016
显然这行不通。我不确定如何正确实现数学,任何建议都会有所帮助。
谢谢!
编辑
再次感谢您的回答。这正是我需要的推动力——显然我不会数学。
我也想用我的完整答案来更新这个......
totalInterest = Math.round(((principal) * Math.pow(1 + loanInterestRate / 365, daysOfInterest * 1)) - principal);
loanNetCost = (principal) + (loanTotalInterest);
alert('You will owe this much money: + loanNetCost');
【问题讨论】:
-
Math.pow()的第一个参数是公式中^左边的数字,第二个参数是右边的数字。 -
请注意,JS 不懂代数。
(foo)(bar)是语法错误,或者至少是尝试调用函数的错误方式。它必须是(foo) * (bar)。 -
非常感谢大家!我非常感谢您的帮助!我只检查了答案,因为它显示了公式。如果有人遇到此线程,他们可以看到出色的示例。
标签: javascript math exponent