Subject: Formatting numbers
Posted By: keso Post Date: 4/6/2006 3:51:27 AM
Hi there,
I have a list box with numbers in it. These numbers have more than ten decimal places and I wish to format them to only two decimal places. I've tried many methods without success.

I would appreciate your assistance in this issue.

Thanks

Reply By: peterasimpson Reply Date: 4/6/2006 4:02:12 AM
I take it that you load the numbers into the list box using a routine. What I would use is the FormatNumber statement.

Example:-
Dim Number as double = FormatNumber(3.14159265, 2)

Now load Number into your list box.

Number will now = 3.14

You can work the above in to your routine some how.

Let me know how you get on.



Reply By: keso Reply Date: 4/6/2006 4:16:09 AM
Thank you for your help. I still have a problem. I am designing an application to calculate interest due on a loan. The following is the code I have written in my list box, which may make things clearer for you:

Private Sub btnInterest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInterest.Click
        lstInterest.Items.Clear()
        If Interval = "Fortnightly" Then
            For P = (Period * 26) - 26 To 0 Step -26
                Interest = (Instalment * 26) - (Instalment / ((1 + CalculatedRate) ^ P) * (1 - (1 + CalculatedRate) ^ -26)) / CalculatedRate
                lstInterest.Items.Add(Interest)

            Next
            lstInterest.Items.Add((Instalment * Period * 26) - Principal)
            FormatNumber(Interest, 2)
        Else
            For P = (Period * 12) - 12 To 0 Step -12
                Interest = (Instalment * 12) - (Instalment / ((1 + CalculatedRate) ^ P) * (1 - (1 + CalculatedRate) ^ -12)) / CalculatedRate
                lstInterest.Items.Add(Interest)
            Next
            lstInterest.Items.Add((Instalment * Period * 12) - Principal)
            FormatNumber(Interest, 2)
        End If
    End Sub

Please note that I have bolded your suggestion.
Thanks

Reply By: peterasimpson Reply Date: 4/6/2006 4:42:32 AM
Try this. I might have the comma 2 ',2' in the wrong place. If so try putting it after the close bracket '), 2'

Private Sub btnInterest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInterest.Click
        lstInterest.Items.Clear()
        If Interval = "Fortnightly" Then
            For P = (Period * 26) - 26 To 0 Step -26
                Interest = (Instalment * 26) - (Instalment / ((1 + CalculatedRate) ^ P) * (1 - (1 + CalculatedRate) ^ -26)) / CalculatedRate
                lstInterest.Items.Add(Interest)

            Next
            lstInterest.Items.Add(FormatNumber(Instalment * Period * 26) - Principal, 2)
        Else
            For P = (Period * 12) - 12 To 0 Step -12
                Interest = (Instalment * 12) - (Instalment / ((1 + CalculatedRate) ^ P) * (1 - (1 + CalculatedRate) ^ -12)) / CalculatedRate
                lstInterest.Items.Add(Interest)
            Next
            lstInterest.Items.Add(FormatNumber(Instalment * Period * 12) - Principal, 2)
            
        End If
End Sub


Go to topic 42512

Return to index page 322
Return to index page 321
Return to index page 320
Return to index page 319
Return to index page 318
Return to index page 317
Return to index page 316
Return to index page 315
Return to index page 314
Return to index page 313