我知道这个帖子很旧,但今天我正在将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,但是将其转换成 PHP 应该很容易。