本文介绍了.NET CORE 中的 Excel RATE 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 .NET CORE 项目中使用 RATE 函数.我想使用一个 Visual Basic 库,但它不适用于 .NET CORE.https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.financial.rate?view=netframework-4.7.2

I tried to use RATE function in .NET CORE project.There is a Visual Basic library I wanted to use but it does not work with .NET CORE. https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.financial.rate?view=netframework-4.7.2

还有其他方法可以使用它还是应该明确计算它?如何?我找不到这个功能的任何解释.

Are there any other ways to use it or should I calculate it explicitly? How? I can't find any explanation of this function.

推荐答案

根据@omajid 评论,我将官方 VB 代码转换为 C#.这就是在不依赖 Microsoft.VisualBasic.dll 的情况下使用 Rate 方法所需的全部内容,而 Microsoft.VisualBasic.dll 在 .NET CORE 中缺少此方法.

According to @omajid comment I transform official VB code to C#.This is all you need to use Rate method without dependency on Microsoft.VisualBasic.dll which is lacking this method in .NET CORE.

private double Rate(double NPer, double Pmt, double PV, double FV = 0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
{
    double dTemp;
    double dRate0;
    double dRate1;
    double dY0;
    double dY1;
    int I;

    // Check for error condition
    if (NPer <= 0.0)
        throw new ArgumentException("NPer must by greater than zero");

    dRate0 = Guess;
    dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
    if (dY0 > 0)
        dRate1 = (dRate0 / 2);
    else
        dRate1 = (dRate0 * 2);

    dY1 = LEvalRate(dRate1, NPer, Pmt, PV, FV, Due);

    for (I = 0; I <= 39; I++)
    {
        if (dY1 == dY0)
        {
            if (dRate1 > dRate0)
                dRate0 = dRate0 - cnL_IT_STEP;
            else
                dRate0 = dRate0 - cnL_IT_STEP * (-1);
            dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
            if (dY1 == dY0)
                throw new ArgumentException("Divide by zero");
        }

        dRate0 = dRate1 - (dRate1 - dRate0) * dY1 / (dY1 - dY0);

        // Secant method of generating next approximation
        dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
        if (Math.Abs(dY0) < cnL_IT_EPSILON)
            return dRate0;

        dTemp = dY0;
        dY0 = dY1;
        dY1 = dTemp;
        dTemp = dRate0;
        dRate0 = dRate1;
        dRate1 = dTemp;
    }

    throw new ArgumentException("Can not calculate rate");
}

private double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
{
    double dTemp1;
    double dTemp2;
    double dTemp3;

    if (Rate == 0.0)
        return (PV + Pmt * NPer + dFv);
    else
    {
        dTemp3 = Rate + 1.0;
        // WARSI Using the exponent operator for pow(..) in C code of LEvalRate. Still got
        // to make sure that they (pow and ^) are same for all conditions
        dTemp1 = Math.Pow(dTemp3, NPer);

        if (Due != 0)
            dTemp2 = 1 + Rate;
        else
            dTemp2 = 1.0;
        return (PV * dTemp1 + Pmt * dTemp2 * (dTemp1 - 1) / Rate + dFv);
    }
}

private const double cnL_IT_STEP = 0.00001;
private const double cnL_IT_EPSILON = 0.0000001;

enum DueDate
{
    EndOfPeriod = 0,
    BegOfPeriod = 1
}

这篇关于.NET CORE 中的 Excel RATE 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:20