|
-
Mar 13th, 2015, 09:34 AM
#1
Thread Starter
New Member
[RESOLVED] Converting from Decimal to Unsigned Decimal
I'm completely new to al this but I'm currently trying to calculate an equation in visual basic but I want the answer to always be positive and a decimal. This is my code so far:
[CODE]
Public Class Recycling_Cost
Private Sub btnAns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAns.Click
Dim sngNum1 As Single
Dim sngNum2 As Single
Dim sngNum3 As Single
Dim sngNum4 As Single
Dim sngNum5 As Single
Dim sngNum6 As Single
Dim sngNum7 As Single
Dim sngAns As ULong
sngNum1 = Val(txtNum1.Text)
sngNum2 = Val(txtNum2.Text)
sngNum3 = Val(txtNum3.Text)
sngNum4 = Val(txtNum4.Text)
sngNum5 = Val(txtNum5.Text)
sngNum6 = Val(txtNum6.Text)
sngNum7 = Val(txtNum7.Text)
sngAns = ((sngNum1 * sngNum2 - sngNum3) * sngNum4) - (sngNum5 * sngNum6 * sngNum7)
lblAns.Text = sngAns
End Sub
Can anyone tell me how to fix this?
-
Mar 13th, 2015, 09:41 AM
#2
Addicted Member
Re: Converting from Decimal to Unsigned Decimal
I don't think you need the ULong. Just leave it as Single and change your calculation line to
Code:
sngAns = System.Math.Abs(((sngNum1 * sngNum2 - sngNum3) * sngNum4) - (sngNum5 * sngNum6 * sngNum7))
 Originally Posted by CatyJane
I'm completely new to al this but I'm currently trying to calculate an equation in visual basic but I want the answer to always be positive and a decimal. This is my code so far:
[CODE]
Code:
Public Class Recycling_Cost
Private Sub btnAns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAns.Click
Dim sngNum1 As Single
Dim sngNum2 As Single
Dim sngNum3 As Single
Dim sngNum4 As Single
Dim sngNum5 As Single
Dim sngNum6 As Single
Dim sngNum7 As Single
Dim sngAns As ULong
sngNum1 = Val(txtNum1.Text)
sngNum2 = Val(txtNum2.Text)
sngNum3 = Val(txtNum3.Text)
sngNum4 = Val(txtNum4.Text)
sngNum5 = Val(txtNum5.Text)
sngNum6 = Val(txtNum6.Text)
sngNum7 = Val(txtNum7.Text)
sngAns = ((sngNum1 * sngNum2 - sngNum3) * sngNum4) - (sngNum5 * sngNum6 * sngNum7)
lblAns.Text = sngAns
End Sub
Can anyone tell me how to fix this?
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Mar 13th, 2015, 09:51 AM
#3
Addicted Member
Re: Converting from Decimal to Unsigned Decimal
One last tip. If it isn't too daunting for you being that you said you're a nooby, try to follow the following code...
Code:
If Not Single.TryParse(txtNum1.Text, sngNum1) OrElse _
Not Single.TryParse(txtNum2.Text, sngNum2) OrElse _
Not Single.TryParse(txtNum3.Text, sngNum3) OrElse _
Not Single.TryParse(txtNum4.Text, sngNum4) OrElse _
Not Single.TryParse(txtNum5.Text, sngNum5) OrElse _
Not Single.TryParse(txtNum6.Text, sngNum6) OrElse _
Not Single.TryParse(txtNum7.Text, sngNum7) OrElse Then
MsgBox("couldn't convert all supplied values to type short")
Exit Sub
End If
I'm suggesting you study this because it is good practice to test each value you get out of a TextBox to ensure it can be converted to a type Single. Depending on just how new you are this may be a bit much to throw at you, if so my apologies. The takes the values from the text box, which are type String, and tries to parse them as Singles and place the resulting values in the variables you defined (ie. sngNum2)
Last edited by thetimmer; Mar 13th, 2015 at 09:52 AM.
Reason: typo's oh my
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
-
Mar 13th, 2015, 09:56 AM
#4
Thread Starter
New Member
Re: Converting from Decimal to Unsigned Decimal
Thank you so much for that! It worked perfectly and now my dissertation doesn't seem so doomed. I'll have a go at the additional code you suggested too
-
Mar 13th, 2015, 10:06 AM
#5
Addicted Member
Re: Converting from Decimal to Unsigned Decimal
good deal, glad to help. Please mark this thread resolved... and good luck with your dissertation.
_____________
Tim
If anyone's answer has helped you, please show your appreciation by rating that answer.
When you get a solution to your issue remember to mark the thread Resolved.
reference links
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|