有谁知道如何在javascript中实现excel CUMIPMT函数。我找到了这个code,但似乎缺少了一些功能。
最佳答案
这是伪代码,您需要对其进行调整:
Object CUMIPMT(double rate, double nper, double pv, double start_period, double end_period, double type)
{
if (rate <= 0)
{
return "#NUM!";
}
if (nper <= 0)
{
return "#NUM!";
}
nper = (int)nper;
if (pv <= 0)
{
return "#NUM!";
}
if (start_period < 1)
{
return "#NUM!";
}
start_period = (int)start_period;
if (end_period < 1)
{
return "#NUM!";
}
end_period = (int)end_period;
if (start_period > end_period)
{
return "#NUM!";
}
if (!(type == 0 || type == 1))
{
return "#NUM!";
}
double value = 0;
for (int per = (int)start_period; per <= (int)end_period; per++)
{
value += IPMT(rate, nper, per, pv, 0, type);
}
return value;
}
double IPMT(double rate, double nper, double period, double pv, double fv, double type)
{
if (rate == 0)
{
return 0;
}
if (type == 1 && period == 1)
{
return 0;
}
double pmt = PMT(rate, nper, pv, fv, 0);
if (type == 0)
{
return (pv * Math.Pow(1.0 + rate, period - 1) + pmt * ((Math.Pow(1 + rate, period - 1) - 1) / rate)) * rate * (-1.0);
}
else
{
double pmt2 = PMT(rate, nper, pv, fv, 1);
return pmt2 - pmt + (pv * Math.Pow(1.0 + rate, period - 2) + pmt * ((Math.Pow(1 + rate, period - 2) - 1) / rate)) * rate * (-1.0);
}
}
double PMT(double rate, double nper, double pv, double fv, double type)
{
if (rate == 0)
{
return (-1.0) * (fv + pv) / nper;
}
else
{
return (-1.0) *
((fv + pv * Math.Pow((1 + rate), nper)) *
rate /
((1 + rate * type) * (Math.Pow((1 + rate), nper) - 1))
);
}
}