Calculation not working correctly
Could somebody please help me out with getting my calculation to work. What I am trying to do is, if textbox31 is a negative number then it needs to subject textbox31 from textbox1 to get a bigger negative number. If textbox 31 is a positive number then it needs to subtract textbox 1 from textbox31 to come come up with the answer which should be positive. I have tried the following code but it seems to only be doing the first part of the equation and not the second ie the answer is always negative. Some help with this would be greatly appreciated. Diane
If TextBox31.Text < 0 Then
Form1.TextBox1.Text = Val(TextBox1.Text) - Val(TextBox31.Text)
Else
Form1.TextBox1.Text = Val(TextBox31.Text) - Val(TextBox1.Text)
EndIf
I have also tried: If Val(TextBox31.Text < 0) Then
but the result is the same
Re: Calculation not working correctly
if you mean the length of text in textbox31 then
Code:
If Len(Textbox31.text) <= 0 then
'your code
End If
Re: Calculation not working correctly
Try this:
Code:
If CInt(TextBox31.Text) < 0 Then
Dim result As Integer = CInt(TextBox31.Text) - CInt(TextBox1.Text)
FormatNumber(result.ToString("n"))
TextBox1.Text = result
Else
Dim result As Integer = CInt(TextBox31.Text) - CInt(TextBox1.Text)
FormatNumber(result.ToString("n"))
TextBox1.Text = result
End If
follow your math closely
-50 minus 50 = -100
-50 plus 50 = 0
Not sure the exact result you were looking for hope this helps you though
Re: Calculation not working correctly
Er, simple algebra. The calculation isn't wrong. Your expectation of it is.
x -(-y) = x + y which by definition can't be a 'bigger negative number'. You need x +(-y) to make the equation work BUT it will still only be a 'bigger negative number' if x is negative.So with your present equations ...
TB1 = 5 and TB31 = -10 : 5 -(-10) = 15
TB1 = 5 and TB31 = 10 : 10 - 5 = 5
TB1 = -5 and TB31 = -10 : -5 -(-10) = 5
TB1 = -5 and TB31 = 10 : 10 -(-5) = 15