PV 函数以及将 VB6 移植到 C#

发布于 2024-08-05 11:43:55 字数 162 浏览 5 评论 0原文

我正在致力于将一些经典的 VB6 代码移植到 C#,并且偶然发现了 PV 函数的用法。

我感觉错误包括对 Microsoft.VisualBasic 程序集的引用。这是常见的做法,还是我应该探索更多选择。我想到的下一个想法是探索 Reflector 中的 PV 功能。

I'm working on porting some classic VB6 code to C# and just stumbled across a usage of the PV function.

I feels wrong including a reference to the Microsoft.VisualBasic Assembly. Is this something that is commonly done, or should I explore further options. The next idea that pops into my mind is exploring this PV function in Reflector.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

厌倦 2024-08-12 11:43:55

C# 和 VB.NET 中 Microsoft.VisualBasic 的使用已在 这个问题。 Microsoft.VisualBasic 命名空间完全受支持,只要 .Net 存在,它就会存在。没有理由避免它。

编辑:这说明在打字时,这个问题的其他答案是函数的不正确重新实现,以及来自代码的单人乐队不受支持的库画廊。来吧伙计们,微软要从 VB 中删除财务功能需要真正的重大事件

对于 Microsoft.VisualBasic.Compatibility 来说,这是一个不同的故事,它专门供 VB6 升级向导使用,编辑 现已在 .Net 4 中标记为过时< /em>(我的预测成真了),并且不应该用于新的开发。删除对此的引用会有一些优势,但就我个人而言,我可能会尝试首先引用 .Net 3.5 来实现完全工作的端口。

The use of Microsoft.VisualBasic from C# and VB.NET has been discussed thoroughly under this question. The Microsoft.VisualBasic namespace is fully supported, and will be around as long as .Net is around. There's no reason to avoid it.

EDIT: It's telling that at the time of typing, the other answers for this question are an incorrect reimplementation of the function, and a one-man-band unsupported library from Code Galleries. Come on guys, it would take a real major event for Microsoft to drop the financial functions from VB.

It's a different story for Microsoft.VisualBasic.Compatibility, which is exclusively for use by the VB6 upgrade wizard, EDIT has now been marked obsolete in .Net 4 (my prediction came true), and should not be used for new development. There would be some advantages in removing references to this, but personally I'd probably try to achieve a fully working port first referencing.Net 3.5.

自控 2024-08-12 11:43:55

在 C# 中复制非常简单,

    public static double PV(double Rate, int nPer, double Pmt, double FV, bool Type)
    {
        double ann = Math.Pow(1 + Rate, nPer);
        return -(FV + Pmt * (1 + (Type ? Rate : 0)) * ((ann - 1) / Rate)) / ann;
    }

只需重新排列 Microsoft 提供的公式< /a>.

Pretty straight forward to replicate in C#

    public static double PV(double Rate, int nPer, double Pmt, double FV, bool Type)
    {
        double ann = Math.Pow(1 + Rate, nPer);
        return -(FV + Pmt * (1 + (Type ? Rate : 0)) * ((ann - 1) / Rate)) / ann;
    }

Just a re-arrangement of the formula Microsoft provides.

相思故 2024-08-12 11:43:55

您可以使用这个库来复制 f# 中的 excel 函数

Library

它的 PV 是现值。我用过一两次。只需将其放入并添加参考即可。

You could use this library that duplicates excel functions in f#

Library

It has PV which is present value. I have used it once or twice. Just drop it in and add the reference.

苏别ゝ 2024-08-12 11:43:55

我尝试使用已接受的答案,但无法让它在 .NET Core 中工作。我环顾四周,在 github 上发现了这段代码,它似乎是 Microsoft 的 PV 实现:

https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/Financial.vb#L662

    Public Function PV(ByVal Rate As Double, ByVal NPer As Double, ByVal Pmt As Double, Optional ByVal FV As Double = 0, Optional ByVal Due As DueDate = DueDate.EndOfPeriod) As Double

        Dim dTemp As Double
        Dim dTemp2 As Double
        Dim dTemp3 As Double

        If Rate = 0.0# Then
            Return (-FV - Pmt * NPer)
        Else
            If Due <> 0 Then
                dTemp = 1.0# + Rate
            Else
                dTemp = 1.0#
            End If
            dTemp3 = 1.0# + Rate

            '       WARSI Using the exponent operator for pow(..) in C code of PV. Still got
            '       to make sure that they (pow and ^) are same for all conditions
            dTemp2 = dTemp3 ^ NPer

            'Do divides before multiplies to avoid OverFlowExceptions
            Return (-(FV + Pmt * dTemp * ((dTemp2 - 1.0#) / Rate)) / dTemp2)
        End If

    End Function

您仍然会需要将其转译为 C#。如果我在本文末尾有一个足够通用的实现,我也会发布它。

I tried using the accepted answer, but I couldn't get it to work in .NET Core. I did some looking around and ran across this code on github that appears to be Microsoft's implementation of PV:

https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/Financial.vb#L662

    Public Function PV(ByVal Rate As Double, ByVal NPer As Double, ByVal Pmt As Double, Optional ByVal FV As Double = 0, Optional ByVal Due As DueDate = DueDate.EndOfPeriod) As Double

        Dim dTemp As Double
        Dim dTemp2 As Double
        Dim dTemp3 As Double

        If Rate = 0.0# Then
            Return (-FV - Pmt * NPer)
        Else
            If Due <> 0 Then
                dTemp = 1.0# + Rate
            Else
                dTemp = 1.0#
            End If
            dTemp3 = 1.0# + Rate

            '       WARSI Using the exponent operator for pow(..) in C code of PV. Still got
            '       to make sure that they (pow and ^) are same for all conditions
            dTemp2 = dTemp3 ^ NPer

            'Do divides before multiplies to avoid OverFlowExceptions
            Return (-(FV + Pmt * dTemp * ((dTemp2 - 1.0#) / Rate)) / dTemp2)
        End If

    End Function

You'd still need to transpile it to C#. If I have a general enough implementation at the end of this I'll post that too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文