代码之家  ›  专栏  ›  技术社区  ›  TruMan1

VB“Financial.Pmt”等价于C#?

  •  2
  • TruMan1  · 技术社区  · 14 年前

    Microsoft.VisualBasic程序集中有一个内置函数。我可以在VB中这样使用它:

    Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoanAmount) * -1
    

    那么,我如何使用C#中的Financial.Pmt(或者甚至将源代码移植到其中)?谢谢你的帮助。

    4 回复  |  直到 14 年前
        1
  •  14
  •   Community CDub    7 年前

    这样地:

    using System;
    using Microsoft.VisualBasic;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                double dAPR = 2;
                Int32 iNumberOfPayments = 12;
                double dLoanAmount = 10000;
                Console.WriteLine(Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoanAmount, 0, DueDate.EndOfPeriod) * -1);
                Console.ReadLine();
            }
        }
    }
    
    • 就像 Joel says
    • 就像Rup在评论中所说的,您必须为第4和第5个参数提供默认值。

    Do use Microsoft.VisualBasic from C# when appropriate . 它是.Net中完全支持的核心库,并且包含一些有用的金融功能。

        2
  •  4
  •   stack247    10 年前

    对于那些谁不喜欢导入VB函数。这是PMT的纯C代码

    public static double PMT(double yearlyInterestRate, int totalNumberOfMonths, double loanAmount)
    {
        var rate = (double) yearlyInterestRate / 100 / 12;
        var denominator = Math.Pow((1 + rate), totalNumberOfMonths) - 1;
        return (rate + (rate/denominator)) * loanAmount;
    }
    

    PMT(7, 360, 120000);
    // Result: 798.36
    PMT(4.5, 360, 137500.47);
    // Result: 696.69
    PMT(4.13, 360, 61520);
    // Result: 298.33
    PMT(6.38, 360, 89200);
    // Result: 556.78
    
        3
  •  2
  •   Chris Taylor    5 年前

    如前所述,您可以使用提供大量VB6功能的Microsoft.VisualBasic程序集。但老实说,如果你是在看更一般的财务计算,你应该考虑看看 Excel Financial Functions for .NET .

    该库可以通过NuGet安装

    PM> Install-Package ExcelFinancialFunctions

        4
  •  1
  •   Joel Coehoorn    14 年前

    和那个一样 using Microsoft.VisualBasic;