c# – “为钱,总是小数”?

前端之家收集整理的这篇文章主要介绍了c# – “为钱,总是小数”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
那么在Microsoft开发团队里面,这个规则是“对于钱,总是十进制”,因为如果是的话:
Namespace: Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Financial.IPmt和所有其他方法将接收/返回十进制,而不是双倍.

现在我想知道如果我能使用这些方法,而不用担心圆错误

我应该使用其他图书馆来处理财务问题吗?如果是的话,你能指点一些好的(C#使用)吗?

解决方法

你可以使用这个类:
public class Financial
{
    #region Methods

    public static decimal IPmt(decimal Rate,decimal Per,decimal NPer,decimal PV,decimal FV,FinancialEnumDueDate Due)
    {
        decimal num;
        if (Due != FinancialEnumDueDate.EndOfPeriod)
        {
            num = 2;
        }
        else
        {
            num = 1;
        }
        if ((Per <= 0) || (Per >= (NPer + 1)))
        {
            //Argument_InvalidValue1=

            throw new ArgumentException("Argument 'Per' is not a valid value.");
        }
        if ((Due != FinancialEnumDueDate.EndOfPeriod) && (Per == 1))
        {
            return 0;
        }
        decimal pmt = Pmt(Rate,NPer,PV,FV,Due);
        if (Due != FinancialEnumDueDate.EndOfPeriod)
        {
            PV += pmt;
        }
        return (FV_Internal(Rate,Per - num,pmt,FinancialEnumDueDate.EndOfPeriod) * Rate);
    }

    public static decimal PPmt(decimal Rate,FinancialEnumDueDate Due)
    {
        if ((Per <= 0) || (Per >= (NPer + 1)))
        {
            throw new ArgumentException("Argument 'Per' is not valid.");
        }
        decimal num2 = Pmt(Rate,Due);
        decimal num = IPmt(Rate,Per,Due);
        return (num2 - num);
    }

    static decimal FV_Internal(decimal Rate,decimal Pmt,FinancialEnumDueDate Due)
    {
        decimal num;
        if (Rate == 0)
        {
            return (-PV - (Pmt * NPer));
        }
        if (Due != FinancialEnumDueDate.EndOfPeriod)
        {
            num = 1 + Rate;
        }
        else
        {
            num = 1;
        }
        decimal x = 1 + Rate;
        decimal num2 = (decimal)Math.Pow((double)x,(double)NPer);
        return ((-PV * num2) - (((Pmt / Rate) * num) * (num2 - 1)));
    }

    static decimal Pmt(decimal Rate,FinancialEnumDueDate Due)
    {
        decimal num;
        if (NPer == 0)
        {
            throw new ArgumentException("Argument NPer is not a valid value.");
        }
        if (Rate == 0)
        {
            return ((-FV - PV) / NPer);
        }
        if (Due != FinancialEnumDueDate.EndOfPeriod)
        {
            num = 1 + Rate;
        }
        else
        {
            num = 1;
        }
        decimal x = Rate + 1;
        decimal num2 = (decimal)Math.Pow((double)x,(double)NPer);
        return (((-FV - (PV * num2)) / (num * (num2 - 1))) * Rate);
    }

    #endregion Methods
}
原文链接:https://www.f2er.com/csharp/95037.html

猜你在找的C#相关文章