在PHP中的Excel公式

5

有人能告诉我这些MS Excel函数背后的数学原理吗?我正在用PHP编写代码,无法使用Excel进行计算,但我仍需要相同的结果。

CUMIPMT(rate,nper,pv,start_period,end_period,type) Returns the cumulative
interest paid on a loan between start_period and end_period.

我的Excel函数 -

=IF($C$33<0,CUMIPMT($C$36/12,$C$35,-$C$33,$C$34,$C$34+11,0),-CUMIPMT($C$36/12,$C$35,$C$33,$C$34,$C$34+11,0))

还有一个问题 - 我使用了(=E11-E12),但它没有减去,而是加上了这两个值,我不明白为什么?

提前感谢您。


什么意思(=E11-E12)不是在减去吗?它在Excel中不起作用吗?它在PHP中不起作用吗?请展示细节。 - Mark Baker
3个回答

2

我没有使用过它,但您可能想查看PHPExcel库。 Excel和PHP的差异足够大,您将需要使用类似于我提供链接的库,或编写函数来手动进行所有计算。在您的情况下,处理Excel文件似乎是实现目标最简单的方法。


谢谢回复。还有一个问题- 我使用了(= E11-E12),但它没有减去,它会将两个值相加,我不明白为什么? - Xi Kam
它可能假设“-”字符表示E11 THROUGH E12列,而不是E11减去E12。但我不能确定,因为我不怎么使用Excel。 - Brendon Dugan
一旦你编写了自己的函数,你就可以使用这个Excel CUMIPMT计算器进行比较和确认结果。http://thinkanddone.co.uk/en-gb/excel_cumipmt_calculator.html - user3177013
谢谢回复,但我的问题仍未得到解答。我需要PHP中CUMIPMT函数背后的代码逻辑。 - Xi Kam
@XiKam,你是怎么做到的?我也在做同样的事情,需要在PHP中计算CUMIPMT而不使用Excel。 - Shairyar

1

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


0

IF( condition, [value_if_true], [value_if_false] )

condition 是您要测试的值。

value_if_true 是可选的。如果条件评估为TRUE,则返回该值。

value_if_false 是可选的。如果条件评估为FALSE,则返回该值。

基本上,它是在说这个:

if ($C$33 < 0) {
    // if the condition is TRUE
    return CUMIPMT($C$36 / 12, $C$35, -$C$33, $C$34, $C$34 + 11, 0);
} else {
    // if the condition is FALSE
    return -CUMIPMT($C$36 / 12, $C$35, $C$33, $C$34, $C$34 + 11, 0);
}

关于您的(=E11-E12)问题。尝试将其更改为=E11-E12。如果E12是负数,您必须记住减号会分配。

编辑: 关于您的评论,希望这个链接有所帮助。

http://www.excelforum.com/excel-programming-vba-macros/515109-need-longhand-formulas-for-cumipmt-and-cumprinc-functions-in-excel.html


谢谢回复,但我的问题仍未得到解答。我需要PHP中CUMIPMT函数的代码逻辑。我尝试了一下,但是没有起作用 -P = 本金(初始投资) r = 年利率(以小数表示) n = 每年复利次数 t = 年数 A = 时间t后的金额A = P(1 + r/n)nt,其中P = 1500,r = 4.3/100 = 0.043, n = 4(不是1/4),t = 6。 - Xi Kam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接