这个问题已经消失一年多了,但我最近需要做同样的事情。这是我想出的:
function pmt(rate_per_period, number_of_payments, present_value, future_value, type){
if(rate_per_period != 0.0){
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if(number_of_payments != 0.0){
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
type
需要是1
or 0
,与 Excel 相同。这rate_per_period
必须是小数(例如:0.25
, not 25%
).
一个例子:
/* Example: */
var interest = 0.07, // Annual interest
years = 5, // Lifetime of loan (in years)
present = 10000, // Present value of loan
future = 20000, // Future value of loan
beginning = 1; // Calculated at start of each period
var payment = -pmt(interest / 12, // Annual interest into months
years * 12, // Total months for life of loan
present,
future,
beginning);
示例期间(月)的付款约为 474.60 美元。
请注意结果的否定,因为该金额是扣除额 - 即:花费您 474 美元 - 结果是负值。如果结果是信用,则结果将是积极的。一般来说,您会希望将其保留为负数/正数,但如果您以如下格式显示它Total Debt: $XYZ
,你想将其转换为正值。