如何在PHP中复制Excel FV函数? [英] How can I replicate the Excel FV function in PHP?

查看:70
本文介绍了如何在PHP中复制Excel FV函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些相当复杂的计算从Excel电子表格转换为PHP.我被困在Excel的FV函数的转换中,该函数是这样定义的:

I'm converting some rather complex calculations from an Excel spreadsheet, to PHP. I'm stuck on the conversion of Excel's FV function, which is defined thusly:

FV( interest_rate, number_payments, payment, PV, Type )

我已经为此工作了2个小时,肯定有一些我想念的东西.本质上,我需要使用上述所有参数,将此功能复制到等效的PHP函数中.

I've been working on this for 2 hours now, and there has to be something I'm missing. Essentially, I need to replicate this functionality into an equivalent PHP function, taking all of the aforementioned arguments.

任何帮助将非常感激.

推荐答案

在PHPExcel函数库中稍作修改:

Slightly modified from the PHPExcel function library:

/**
 * FV
 *
 * Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
 *
 * @param   float   $rate   Interest rate per period
 * @param   int     $nper   Number of periods
 * @param   float   $pmt    Periodic payment (annuity)
 * @param   float   $pv     Present Value
 * @param   int     $type   Payment type: 0 = at the end of each period, 1 = at the beginning of each period
 * @return  float
 */
function FV($rate = 0, $nper = 0, $pmt = 0, $pv = 0, $type = 0) {

    // Validate parameters
    if ($type != 0 && $type != 1) {
        return False;
    }

    // Calculate
    if ($rate != 0.0) {
        return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
    } else {
        return -$pv - $pmt * $nper;
    }
}   //  function FV()


echo FV(0.0149562574418, 4, 43.875, -250);

返回85.818510876629

returns 85.818510876629

//单元测试

class ExcelTest extends \PHPUnit_Framework_TestCase
{

    public function test_it_calculates_fv_value()
    {
        $test_data = [
            [ 0.005,          10, -200,  -500,    1,    2581.4033740601 ],
            [ 0.01,           12, -1000, null,    null, 12682.503013197 ],
            [ 0.009166666667, 35, -2000, null,    1,    82846.246372418 ],
            [ 0.005,          12, -100,  -1000,   1,    2301.4018303409 ],
            [ 0.004166666667, 60, -1000, null,    null, 68006.082841536 ],
            [ 0.025,          16, -2000, 0,       1,    39729.460894166 ],
            [ 0.0,            12, -100,  -100,    null, 1300            ]
        ];

        $test_case_id = 0;
        foreach($test_data as $test_case) {
            $test_case_id++;
            list($rate, $nper, $pmt, $pv, $type, $expected_result) = $test_case;
            $this->assertEquals($expected_result, Excel::FV($rate, $nper, $pmt, $pv, $type), "Test case $test_case_id failed", 0.0000001);
        }
    }

}

这篇关于如何在PHP中复制Excel FV函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆