首页 > 未分类 > 贷款利率
2024
07-09

贷款利率

贷款50000 分12期 每期4247 每月利息80.3 一共连本带息50964

年利率多少?

贷款利率 - 第1张  | bono504 技术生活站点

C#计算

using System;

class Program
{
    static void Main()
    {
        double loanAmount = 50000;
        double[] cashFlows = new double[13];
        cashFlows[0] = -loanAmount;
        for (int i = 1; i <= 12; i++)
        {
            cashFlows[i] = 4247;
        }

        double monthlyRate = CalculateIRR(cashFlows);
        double annualRate = Math.Pow(1 + monthlyRate, 12) - 1;

        Console.WriteLine($"年利率为: {annualRate * 100:F2}%");
        Console.ReadKey();
    }

    static double CalculateIRR(double[] cashFlows, double guess = 0.1)
    {
        double x0 = guess;
        double x1 = 0;
        double tolerance = 1e-6;
        int maxIterations = 1000;
        int iteration = 0;

        while (iteration < maxIterations)
        {
            double fValue = 0;
            double fDerivative = 0;

            for (int i = 0; i < cashFlows.Length; i++)
            {
                fValue += cashFlows[i] / Math.Pow(1 + x0, i);
                fDerivative += -i * cashFlows[i] / Math.Pow(1 + x0, i + 1);
            }

            x1 = x0 - fValue / fDerivative;

            if (Math.Abs(x1 - x0) < tolerance)
            {
                break;
            }

            x0 = x1;
            iteration++;
        }

        return x1;
    }
}

也不算很低吧

最后编辑:
作者:bono
这个作者貌似有点懒,什么都没有留下。

贷款利率》有 2 条评论

留下一个回复

你的email不会被公开。