我知道这个线程很旧,但是我今天正在将 Excel 电子表格转换为 Objective-c,并且也需要对此的答案。这是我最终写的解决方案:
double cumipmt(double rate, int nper, double pv, int start_period, int end_period, BOOL type)
{
double cumipmt = 0;
double thePmt = pmt(rate, nper, pv, 0, type);
double endValue = pv;
if (type==0)
{
for (int i=1; i<=end_period; i++)
{
double beginning = endValue;
double interest = beginning * rate;
endValue = beginning + interest + thePmt;
if (i>=start_period)
{
cumipmt += interest;
}
}
}
else
{
for (int i=1; i<end_period; i++)
{
double beginning = endValue + thePmt;
double interest = beginning * rate;
endValue = beginning + interest + thePmt;
if (i>=start_period)
{
cumipmt += interest;
}
}
}
return cumipmt;
}
double pvFactor(double rate, int nper)
{
return (1 / fvFactor(rate, nper));
}
double fvFactor(double rate, int nper)
{
return pow(1+rate,nper);
}
double annuityPvFactor(double rate, int nper, bool type)
{
if (rate == 0) {
return nper;
}
else {
return (1 + rate * type) * (1 - pvFactor(rate, nper)) / rate;
}
}
double pmt(double rate, int nper, double pv, double fv, bool type)
{
return (-1 * (pv + fv + pvFactor(rate, nper))) / (annuityPvFactor(rate, nper, type));
}
我知道这不在 PHP 中,但转换起来应该很容易。