Dry*_*ods 10 c# vb.net types finance financial
好吧,规则" For money,always decimal "不适用于Microsoft开发团队,因为如果它是:
Namespace: Microsoft.VisualBasic
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
Run Code Online (Sandbox Code Playgroud)
Financial.IPmt并且所有其他方法都将接收/返回decimal而不是double原样.
现在我想知道我是否可以使用这些方法而不用担心圆形错误?
我应该使用其他一些图书馆来处理财务问题吗?如果是的话,你能指点一些好的(供C#使用)吗?
Dan*_*fer 10
以下是关于这个主题的有趣讨论:http://www.vbforums.com/showthread.php? t = 524101
大约1/3的人解释说它使用Double,因为VB.NET函数的实现与VB6完全相同.VB6没有十进制类型,这就是它使用double的原因.
因此,似乎如果准确性很重要,则不应使用这些功能.
这个问题的答案有一些很有希望的替代方案 - 只是忽略了建议使用VB库的接受答案.
以前链接的问题已被删除,所以这里有一些我引用的建议(注意:我没试过这些,YMMV)
Jef*_*tin 10
decimal用于赚钱的规则很有用,因为大多数货币都有十进制单位.通过使用十进制算术,可以避免引入和累积舍入误差.
Financial Class函数使用浮点数有以下几个原因:
Pmt并且舍入可以确定名义上的每月付款,但是一旦确定了该金额,就会发生余额累积 - 付款,应用的利息等等decimal.此外,延迟或预付款,支付假期和其他此类不统一性将使财务职能部门提供的预计摊销无效.
你可以使用这个类:
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, PV, FinancialEnumDueDate.EndOfPeriod) * Rate);
}
public static decimal PPmt(decimal Rate, decimal Per, decimal NPer, decimal PV, decimal FV, FinancialEnumDueDate Due)
{
if ((Per <= 0) || (Per >= (NPer + 1)))
{
throw new ArgumentException("Argument 'Per' is not valid.");
}
decimal num2 = Pmt(Rate, NPer, PV, FV, Due);
decimal num = IPmt(Rate, Per, NPer, PV, FV, Due);
return (num2 - num);
}
static decimal FV_Internal(decimal Rate, decimal NPer, decimal Pmt, decimal PV, 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, decimal NPer, decimal PV, decimal FV, 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
}
Run Code Online (Sandbox Code Playgroud)