Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code has problem in ndays. i dont know if im doing right.

VB
Private Sub ComputeLedger()
        Dim interest = txtIntRate1.Text
        Dim interest2 = txtIntRate2.Text
        Dim princiapalLoan = txtBalAmortize.Text
        Dim matValue = txtMoAmort1.Text
        Dim matValue2 = txtMoAmort2.Text
        Dim dblMat As Double = Decimal.Parse(matValue, Globalization.NumberStyles.Currency)
        matValue = dblMat.ToString(Format("###,###,###.#0", "Standard"))
        Dim dblMat2 As Double = Decimal.Parse(matValue2, Globalization.NumberStyles.Currency)
        matValue2 = dblMat2.ToString(Format("###,###,###.#0", "Standard"))

        Dim effDate = dtpValueDate.Value
        Dim dueDate = dtpFirstDueDate.Value
        Dim firstdueDate = DateTimePicker1.Value
        Dim secondeffDate = DateTimePicker2.Value

        Dim i As Integer
        Dim weekInt As Integer
        Dim weekInt3 As Integer
        Dim weekInt2 As Integer = Val(txtTerms2.Text)
        Dim durations As Integer = Val(txtTerms1.Text)
        Dim durations2 As Integer = Val(lblTotalTerms.Text)
        'DataGridView1.Rows.Clear()

        Dim ndays As Integer
        ndays = DateDiff("d", firstdueDate, secondeffDate)

        Dim areturnv As Double '= (princiapalLoan / durations2)
        Dim dblret2 As Double = Decimal.Parse(areturnv, Globalization.NumberStyles.Currency)
        Dim ainterest As Double '= ((princiapalLoan * (interest2)) * (ndays / 365))
        Dim dblint2 As Double = Decimal.Parse(ainterest, Globalization.NumberStyles.Currency)
        Dim princ As Double '= (matValue - ainterest)
        Dim dblprin2 As Double = Decimal.Parse(princ, Globalization.NumberStyles.Currency)
        Dim tolamt = (princiapalLoan)
        Dim dblamt2 As Double = Decimal.Parse(tolamt, Globalization.NumberStyles.Currency)
        Dim tolamt3 As Double '= (princiapalLoan - princ)
        Dim dblamt3 As Double = Decimal.Parse(tolamt3, Globalization.NumberStyles.Currency)
        'Dim tolamt4 As Double '= (princiapalLoan - princ)
        Dim dblamt4 As Integer 'Double = Decimal.Parse(tolamt4, Globalization.NumberStyles.Currency)

       Do While durations < durations2
            durations += 1
            weekInt3 += 1

            secondeffDate = firstdueDate.AddMonths(weekInt3)
            If secondeffDate.DayOfWeek = DayOfWeek.Saturday Then
                secondeffDate = secondeffDate.AddDays(2)
            ElseIf secondeffDate.DayOfWeek = DayOfWeek.Sunday Then
                secondeffDate = secondeffDate.AddDays(1)
            End If

            Select Case dblamt4
                Case "01" 'January"
                    dblamt4 = "31"
                Case "02" 'February"
                    dblamt4 = "28"
                Case "03" 'March"
                    dblamt4 = "31"
                Case "04" 'April"
                    dblamt4 = "30"
                Case "05" 'May"
                    dblamt4 = "31"
                Case "06" 'June"
                    dblamt4 = "30"
                Case "07" 'July"
                    dblamt4 = "31"
                Case "08" 'August"
                    dblamt4 = "31"
                Case "09" 'September"
                    dblamt4 = "30"
                Case "10" 'October"
                    dblamt4 = "31"
                Case "11" 'November"
                    dblamt4 = "30"
                Case "12" 'December"
                    dblamt4 = "31"
            End Select

'my problem is in code. 
            ndays = DateDiff(DateInterval.Day, firstdueDate.AddDays(dblamt4), firstdueDate.AddMonths(weekInt3))
            dblret2 = ((princiapalLoan * (interest2)) * (ndays / 365))

            dblint2 = dblMat2 - dblret2
            dblamt3 = dblamt3 - dblint2

            DataGridView1.Rows.Add(secondeffDate.ToString("MM/dd/yyyy"), _
                                    dblMat2.ToString(Format("###,###,###.#0", "Standard")), _
                                    ndays, _
                                    dblret2.ToString(Format("###,###,###.#0", "Standard")), _
                                    dblint2.ToString(Format("###,###,###.#0", "Standard")), _
                                    dblamt3.ToString(Format("###,###,###.#0", "Standard")))
        Loop


    End Sub
Posted
Updated 2-Oct-14 15:21pm
v2
Comments
[no name] 2-Oct-14 21:38pm    
And how would we know what you think is "right"? We don't know what it is that you are doing or what it is that you see on your screen.
Member 9663328 2-Oct-14 21:55pm    
please check this link it has picture of my code.

http://www.dreamincode.net/forums/uploads/monthly_10_2014/post-650638-141229598642.png
Sinisa Hajnal 3-Oct-14 2:22am    
That still doesn't explain what you need...please state your problem clearly.
We see the column number of days, but not what is wrong...what you expect to be there based on what calculation. Narrow down your problem and edit the question to describe only that problem.

Thank you
Member 9663328 3-Oct-14 2:26am    
if you'll see in the column of No of Days

31 - 31
61 - 30
92 - 31



The number of days should not be adding. it should be 30,31. 28 or 29 if leap year depends on the due date from the previous day.

1 solution

That's easy:

You have:
VB
ndays = DateDiff(DateInterval.Day, firstdueDate.AddDays(dblamt4), firstdueDate.AddMonths(weekInt3))


Where you're doing difference in days between firstdueDate and some fixed amount (say 31)

And your weekInt3 is increasing with each iteration so you have difference between fixed number and increasing number of months

I think you need to use secondeffDate in that calc...

That said, I didn't read whole of your code, just that last loop with ndays so maybe I'm missing something.

Even if it is not secondeffDate, it definitely is something along that line.

Finally, while your explanation explains what is wrong, it doesn't give anything about what it SHOULD do so I cannot say definitely WHAT the solution is, just the reason why you have increasing difference.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900