1
A
回答
0
我相信這是在C#中的正確實施:
public double XNPV(decimal[] receipts,DateTime[] dates, double dRate, DateTime issueDate, decimal cf, int x)
{
double sum;
for(int i =0;i<dates.Length;i++)
{
TimeSpan ts = dates[i].Subtract(issuedate);
sum +=receipts[i]/((1 + dRate/cf)^((ts.TotalDays/360) * cf));
}
return sum;
}
,這是在VB中等價的:
Public Function XNPV(ByVal Receipts() As Decimal, ByVal Dates() As Date, ByVal dRate As Double, ByVal IssueDate As Date, ByVal CF As Decimal, ByVal x As Integer) As Double
Dim i As Integer
Dim sum As Double
For i = 0 To UBound(Dates)
sum = sum + Receipts(i)/((1 + dRate/CF)^((DateDiff/360) * CF))
XNPV = sum
End Function
0
我適應了上面的代碼,幷包含一個如何調用它的例子。它應該很容易適應使用可變貼現率,而不是0.1
Public Function XNPV(ByVal Receipts() As Decimal, ByVal Dates() As Date, ByVal dRate As Double, ByVal StartDate As Date) As Double
Dim i As Integer = 0
Dim dXNPV As Double = 0
Dim dXRate As Double = 1 + dRate
Dim dDayOfCF As Long = 0
Dim dAdj As Double = 0
Dim dResult As Double = 0
For i = 0 To UBound(Dates)
dDayOfCF = (DateDiff(DateInterval.Day, StartDate, Dates(i)))
dAdj = (dDayOfCF/365)
dXNPV = Receipts(i)/(dXRate^dAdj)
dResult += dXNPV
Next
XNPV = dResult
End Function
Private Sub SimpleButton2_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton2.Click
Dim tblReceipts(2) As Decimal
Dim tblDates(2) As DateTime
Dim dtStartDate As DateTime = "1/1/13"
Dim XNPVResult As Decimal = 0
tblReceipts(0) = -100000
tblReceipts(1) = 500000
tblReceipts(2) = 2000000
tblDates(0) = "1/1/13"
tblDates(1) = "1/7/13"
tblDates(2) = "1/1/15"
'xCF = 1 + DateDiff(DateInterval.Day, dtIssueDate, tblDates(2))
XNPVResult = XNPV(tblReceipts, tblDates, 0.1, dtStartDate)
MsgBox(XNPVResult)
End Sub
0
返回值一樣擅長XNPV(雙重檢查)
public static double XNPV(double rate, List<xnpvFlow> Cashflows, DateTime? StartDate = null)
{
if (Cashflows == null || Cashflows.Count == 0) return 0;
if(StartDate == null)
{
StartDate = (from xnpvFlow flow in Cashflows select flow.FlowDate).Min();
}
double _xnpv = 0;
foreach(xnpvFlow flow in Cashflows)
{
_xnpv += (double)flow.FlowAmount/Math.Pow((1 + rate), (double)(flow.FlowDate - (DateTime)StartDate).Days/365);
}
return _xnpv;
}
public class xnpvFlow
{
public xnpvFlow(DateTime _FlowDate, decimal _flowAmount)
{
this.FlowAmount = _flowAmount;
this.FlowDate = _FlowDate;
}
public DateTime FlowDate;
public decimal FlowAmount;
}
相關問題
- 1. 計算使用CryptCATAdminCalcHashFromFileHandle在VB
- 2. C#中的Excel計算#
- 3. 在excel中計算未使用的IP
- 4. VB或宏,以排除在Excel中計算持續時間的時間段
- 5. 使用C計算在Excel中的單元格的總和#
- 6. VB代碼不計算
- 7. 使用python在excel中計算值
- 8. 使用c#在excel中啓用迭代計算模式?
- 9. VB的DataGridView計算?
- 10. 使用C#在Excel表中計算行數(包括空行)
- 11. 使用C#或VB在Visual Studio
- 12. 使用VB在excel中求和列使用VB
- 13. 數學計算在vb
- 14. C#中的Excel公式計算#
- 15. 如何使用ASP.NET,VB或C#填充Excel模板?
- 16. 多線程DataTable計算只讀VB或C#
- 17. 試圖在C中使用網格計算或分佈式計算#
- 18. C#計算Excel單元
- 19. VB.net需要XNPV
- 20. 在Excel的單元格中使用VB或宏創建excel附件對象
- 21. 移調在Excel中使用VB
- 22. 在VB中使用Excel函數
- 23. 在不使用VB代碼的情況下在Excel中排序
- 24. 如何在vb datatgridview中進行計算
- 25. 使用VB的Excel轉置
- 26. 計算使用C#
- 27. 在進行計算時,VB不會返回與MS Excel相同的值?
- 28. 我想在Excel中計算
- 29. 在Excel中計算時間
- 30. 如何在Excel中計算