Reputation: 75
I'm adding two decimal numbers. Whenever the fractional part gets to 0.60 it should be rounded up, for example 20.60 is rounded up to 21.00.
I've been able to do that, and the application is mostly working, but whenever the number before the decimal point exceeds the hundreds column, lets say 999.40, and it gets to the thousands column, the thousands separator doesn't work. For example, if I want to add two numbers, say 600.20 and 600.30, instead of my answer being 1,200.50 I get 1.00.
This is my code so far:
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
'ans for txtbox5.tetx
Try
If Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label41.Text).ToString("N2")), 2)) + Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label42.Text).ToString("N2")), 2)) > 99 Then
MessageBox.Show("invalid entry")
Else
Label41.Text = Val(TxtBox2.Text)
Label42.Text = Val(TxtBox34.Text)
'sum of numbers in two txtbox
'TxtBox5.Text = Val(TxtBox2.Text) + Val(TxtBox34.Text)
Label43.Text = (Val(Label41.Text) + Val(Label42.Text)).ToString("N2")
'strip last 2 decimals :
ln = Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label43.Text).ToString("N2")), 2))
Label44.Text = ln.ToString
'form decimal from 2 decimals
Label45.Text = "0." & Label44.Text
'subtract new decimal from 1st answer
Label46.Text = (Val(Label43.Text) - Val(Label45.Text)).ToString("N2")
'checks if striped decimal is btw 100 and 59
If (Val(Label44.Text)) < 100 And (Val(Label44.Text)) > 59 Then
runup = runup + 1
newans = (Val(Label44.Text) - Val(60))
Label45.Text = (Val(Label45.Text) - Val(0.6)).ToString("N2")
Try
'check if decimal is between 100 and 59
If (Val(newans)) < 100 And (Val(newans)) > 59 Then
runup = runup + 1
newans = (Val(newans) - Val(60))
Label45.Text = (Val(Label45.Text) - Val(0.6)).ToString("N2")
Label47.Text = (Val(runup) + Val(Label46.Text)) + Val(Label45.Text).ToString("N2")
runup = 0
'check if new decimal is between 60 and 0
ElseIf (Val(newans)) < 60 And (Val(newans)) >= 0 Then
Label47.Text = ((Val(runup) + Val(Label46.Text)) + Val(Label45.Text)).ToString("N2")
runup = 0
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'check if striped decimal is btw 60 and 0
ElseIf (Val(Label44.Text)) < 60 And (Val(Label44.Text)) >= 0 Then
Label47.Text = ((Val(runup) + Val(Label46.Text)) + Val(Label45.Text)).ToString("N2")
runup = 0
End If
End If
Catch ex As Exception
End Try
TxtBox5.Text = Label47.Text
What is causing the problem with the larger numbers?
Upvotes: 0
Views: 47
Reputation: 25013
It looks like you are making your code harder to understand by using UI elements (e.g. labels) as if they were variables. This is not helped by the names of those labels - how are you meant to figure out what, say, "Label44" represents?
You can take the input data from the textboxes and parse that into a suitable data type - I recommend Decimal for this particular case - and using suitable variable names will make it easier to see what the code is doing.
As an example, I put three textboxes on a form and named them "tbNum1", "tbNum2", and "tbSum", added a button named "bnCalculate", and used this code:
Private Sub bnCalculate_Click(sender As Object, e As EventArgs) Handles bnCalculate.Click
Dim num1 As Decimal = Decimal.Parse(tbNum1.Text)
Dim num2 As Decimal = Decimal.Parse(tbNum2.Text)
Dim sum = num1 + num2
Dim frac = sum - Math.Floor(sum)
If frac >= 0.6 Then
sum = Math.Ceiling(sum)
End If
tbSum.Text = sum.ToString("N2")
End Sub
The code would need to be modified to work correctly for negative numbers.
I could not discern what some of your code was intended to do, e.g. is the first check intended to restrict the number of decimal places entered? There are cleaner ways to do that.
N.B. Do not use Try...Catch
with an empty Catch
part because it will hide problems from you.
Upvotes: 1