Click here to Skip to main content
14,601,060 members
Rate this:
Please Sign up or sign in to vote.
See more:
I seem to have a small bug somehow but for the life of me I cannot fix it, I have a small routine that adds some numbers from Textbox inputs, this works fine until the maths reaches 999.99 then it goes back to 1.00, I have tried different formats but to no avail, my code is.
Public Sub DisplayTotalBalance()
       Dim AddTotalReceipts As Double = 123456.123
       Dim MinusTotalPayments As Double = 123456.123
       Dim GetTotalBalance As Double = 123456.123

       AddTotalReceipts = CDbl(Val(txtTotalReceipts.Text)).ToString("###,###.00")
       MinusTotalPayments = CDbl(Val(txtTotalPayments.Text)).ToString("###,###.00")

  GetTotalBalance = AddTotalReceipts.ToString("###,###.00") - MinusTotalPayments.ToString("###,###.00")
       txtBalance.Text = GetTotalBalance.ToString("###,###.00")

          End Sub

Any idea what i am doing wrong or why it will not add more than 999.99 would be greatly appreciated.
Posted
Updated 23-Dec-11 0:04am
v2
Comments
[no name] 23-Dec-11 6:11am
   
Try this:
GetTotalBalance = AddTotalReceipts - MinusTotalPayments
txtBalance.Text = GetTotalBalance.ToString("###,###.00")
Rate this:
Please Sign up or sign in to vote.

Solution 1

Never, ever, try to do maths with strings.
You have the numbers as Doubles - use those:

GetTotalBalance = AddTotalReceipts - MinusTotalPayments
txtBalance.Text = GetTotalBalance.ToString("###,###.00")
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

All those ToStrings are really not necessary.
You have three doubles which are already numeric and do not need any format.
You can calculate with these numeric types and only in the end do you need to format it for output.
What's also wrong, as far as I can see, if txtTotalReceipts.Text or txtTotalPayments is not convertible to a numeric type your code is going to throw an exception.
You should check the TryParse Method[^].
Public Sub DisplayTotalBalance()
    Dim addTotalReceipts As Double
    Dim minusTotalPayments As Double
    Dim getTotalBalance As Double
    
    If Double.TryParse(txtTotalReceipts.Text, addTotalReceipts) AndAlso Double.TryParse(txtTotalPayments.Text, minusTotalPayments) Then
        getTotalBalance = addTotalReceipts - minusTotalPayments
        txtBalance.Text = getTotalBalance.ToString("###,###.00")
    Else
        ' One of the two textboxes text could not be converted to double.
        txtBalance.Text = String.Empty
    End If
End Sub

Hope it helps :)
   
Comments
Member 8365433 23-Dec-11 8:00am
   
Hi Thanks for the reply however this has not solved my problem, it has to be something silly but dont know what, so i will post the whole code

Public Class Form1

Public Sub CalculateRegFee()

Dim Attendance As Integer
Dim CurrentFee As Double
Dim RegFee As Double

Attendance = txtNoOfReg.Text
CurrentFee = lblRegFee.Text
RegFee = CurrentFee * Attendance '.ToString("###,###.00")
txtRegFee.Text = RegFee.ToString("###,###.00") 'converts the display to 2 decimal places

End Sub

Public Sub CalculateAffiliationFee()

Dim HowMany As Integer 'qty of affiliations
Dim CurrentAffilFee As Double 'current fee
Dim AffilFee As Double 'total fee to be displayed qty times by current fee

HowMany = txtQtyAffilFee.Text
CurrentAffilFee = lblAffilFee.Text
AffilFee = CurrentAffilFee * HowMany
txtAffilFee.Text = AffilFee.ToString("###,###.00") 'converts the display to 2 decimal places

End Sub

Public Sub CalculateInitiationFee()
Dim InitHowMany As Integer
Dim InitCurrentFee As Double
Dim InitFee As Double

InitHowMany = txtQtyInitFee.Text
InitCurrentFee = lblInitiationFee.Text
InitFee = InitCurrentFee * InitHowMany '.ToString("###,###.00")
txtInitiationFee.Text = InitFee.ToString("###,###.00") 'converts the display to 2 decimal places

End Sub

Public Sub CalculateTotalPayments()

Dim PayCol1 As Double 'col 1 Salaries and rent col txtPmntSalaries
Dim PayCol2 As Double 'col 2 Goods col txtPmntGoods
Dim PayCol3 As Double 'col 3 Postage col txtPmntPostage

Dim TotalPayments As Double 'the sub total col that adds it all up txtTotalReceipts

PayCol1 = CDbl(Val(txtPmntSalaries.Text)).ToString("###,###.00") 'col 1
PayCol2 = CDbl(Val(txtPmntGoods.Text)).ToString("###,###.00")
PayCol3 = CDbl(Val(txtPmntPostage.Text)).ToString("###,###.00")

TotalPayments = PayCol1 + PayCol2 + PayCol3 'the maths
txtTotalPayments.Text = TotalPayments.ToString("###,###.00") 'col 7 sub total general fund

End Sub

Public Sub CalculateTotalReceipts()

Dim Col1 As Double 'col 1 reg fee col txtRegFee
Dim Col2 As Double 'col 2 Affiliations fees col txtAffilFee
Dim Col2a As Double 'col 2 Initiations fees col txtInitiationFee

Dim TotalReceipts As Double 'the sub total col that adds it all up txtTotalReceipts

Col1 = CDbl(Val(txtRegFee.Text)).ToString("###,###.00") 'col 1
Col2 = CDbl(Val(txtAffilFee.Text)).ToString("###,###.00")
Col2a = CDbl(Val(txtInitiationFee.Text)).ToString("###,###.00")

TotalReceipts = Col1 + Col2 + Col2a 'the maths
txtTotalReceipts.Text = TotalReceipts.ToString("###,###.00")

End Sub

Public Sub DisplayTotalBalance()
'WORKING
Dim addTotalReceipts As Double
Dim minusTotalPayments As Double
Dim getTotalBalance As Double

If Double.TryParse(txtTotalReceipts.Text, addTotalReceipts) AndAlso Double.TryParse(txtTotalPayments.Text, minusTotalPayments) Then
getTotalBalance = addTotalReceipts - minusTotalPayments
txtBalance.Text = getTotalBalance.ToString("###,###.00")
Else
' One of the two textboxes text could not be converted to double.
txtBalance.Text = String.Empty
End If

End Sub

'====Col 1 Reg Fees
Private Sub txtNoOfReg_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNoOfReg.TextChanged
CalculateRegFee()
End Sub

Private Sub txtRegFee_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Sander Rossel 23-Dec-11 8:18am
   
It seems you are doing the same in the other Calculate Methods. Get rid of the CDbl() and ToStrings. They clutter up your code an are not necessary and actually plain wrong. Try to fix them with the method I proposed, using TryParse and only using ToString when actually showing your output to the user.
Good luck!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100