How to get my total to update correctly in vb.net (using textchanged event)
What i have in a text box that is the discount. The text box talks to the label as you type and updates it accordingly
The problem is that once the discount_rate.text get to 10 or above the discount is off by 5 cents and increases as the number goes up..
Can anybody tell me why?
Private Sub disc开发者_C百科ount_rate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles discount_rate.TextChanged
Select Case radio
Case "one"
If discount_rate.Text = "" Then
Label17.Text = FormatCurrency(a_total.Text * 50)
Label19.Text = FormatCurrency(Label17.Text * 0.06)
a = CDbl(Label17.Text)
b = CDbl(Label19.Text)
Label21.Text = FormatCurrency(a + b)
Else
discount = discount_rate.text / 100
discountrate = Label17.Text * discount
dis_count1.Text = FormatCurrency(discountrate)
Label17.Text = FormatCurrency((a_total.Text * 50) - discountrate)
Label19.Text = FormatCurrency(Label17.Text * 0.06)
a = CDbl(Label17.Text)
b = CDbl(Label19.Text)
Label21.Text = FormatCurrency(a + b)
First of all never use floating point for currency. Double and float are floating point. If you don't know why that is then you need to read this.
Use Decimal instead.
In your case that would make it something like this.
Dim _total as Decimal = Convert.ToDecimal(a_total.Text)
Dim _ihavenoidea = Convert.ToDecimal(Label17.Text)
Label17.Text = FormatCurrency(_total * 50)
Label19.Text = FormatCurrency(ihavenoidea * 0.06)
a = Convert.ToDecimal(Label17.Text)
b = Convert.ToDecimal(Label19.Text)
Label21.Text = FormatCurrency(a + b)
And there is also something to be said for using descriptive variable and control names, the person that will have to maintain your code will thank you for it.
精彩评论