# division in VB

I have set up a simple calculator in VB, you type in two numbers select a +,-,*, or / then hit = and it shows the answer. but for the division part i need it to show the answer but not as a decimal and also i need to show the remainder. I know that i should use mod to do the remainder but im having issues with the decimal issue.

thank you

Tyler
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chief Technology OfficerCommented:
Try something like this:
+Divide the two values and take the floor which will give you just the integer whole number.
+Use mod as you indicated to get the integer remainder.
`````` Dim x As Decimal = 100
Dim y As Decimal = 13

Dim intQuotientWholeNbr As Integer = Math.Floor(x / y)
Dim intQuotientRemainder As Integer = x Mod y

MsgBox(String.Format("{0} divided by {1} is {2} with a remainder of {3}.", x, y, intQuotientWholeNbr, intQuotientRemainder))
``````
0
Author Commented:

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If Label1.Text = "+" Then
Lbl3.Text = Val(TxtBox1.Text) + Val(TxtBox2.Text)
End If
If Label1.Text = "-" Then
Lbl3.Text = Val(TxtBox1.Text) - Val(TxtBox2.Text)
End If
If Label1.Text = "*" Then
Lbl3.Text = Val(TxtBox1.Text) * Val(TxtBox2.Text)
End If
If Label1.Text = "/" Then
Lbl3.Text = Val(TxtBox1.Text) / Val(TxtBox2.Text)
End If
If Label1.Text = "/" Then
LblR.Text = Val(TxtBox1.Text) Mod Val(TxtBox2.Text)
End If
End Sub
0
Commented:
Especially in a calculator app, when you could potentially have *any* values entered for calculation, the Mod operator is not very useful. First, if you are only returning a integer representation of the remainder, then your calculator is going to be extremely inaccurate.
Second, Mod performs type widening when it encounters floating point operands, so the type it returns could be as wide as decimal (the widest floating point class in the language) or as narrow as single. The implicit cast, again, loses so much precision that the result would simply be incorrect.
If you need the integer portion of a floating point operation and a seperate value containing the fractional value, you would need to use at least a Single object to represent the fraction. Something like:

``````Dim x, y As Decimal 'The operands
Dim a As Integer 'The integer portion
Dim b As Single 'The fractional portion

a = CInt(Decimal.Truncate(x / y))
b = (x / y) - a
``````
0
DeveloperCommented:
Hello, VinnyG3,

The suggestion made by mwvisa1 looks just right to me.  Why not use it?

Cheers,
Randy
0
Commented:
The suggestion made by mwvisa1 looks just right to me.  Why not use it?
Really? Replace 13 with 13.25 and see if that rounded number seems like good calculator accuracy (7 with a remainder 7).
0
Chief Technology OfficerCommented:

I would suggest you make sure the values are valid to begin with and store them in variables of type close to what we suggested above do your calculations then handle write back to fields.

This will make your application flow more like:
'input
'process / calc
'output
``````Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim op As Char = Label1.Text.Trim.First()
Dim x, y, resultA, resultB As Decimal
Dim showResultB As Boolean = false

Decimal.TryParse(TxtBox1.Text, x)
Decimal.TryParse(TxtBox2.Text, y)

'consider using else if logic also
If op = "+" Then
resultA = x + y
End If
If op = "-" Then
resultA = x - y
End If
If op = "*" Then
resultA = x * y
End If
If op = "/" Then
resultA = Math.Floor(x / y)
resultB = (x / y) - resultA 'per ladarling suggestion
showResultB = true
End If

Lbl3.Text = resultA
If showResultB Then LblR.Text = resultB
End Sub
``````
0
Chief Technology OfficerCommented:
ladarling, I was only showing that way as asker said that is what was using and not doing any rounding for the MOD, I am doing Math.Floor (round down) on the initial quotient to easily extract the integer whole number from the response.  I was answering Vinny's question of getting rid on the whole number...

BTW, to solve that problem with original response, just change the data type of remainder as I am not doing any math that would drop decimal there so you get right answer.
``````Module Module1

Sub Main()
Dim x As Decimal = 100.0
Dim y As Decimal = 13.25

Dim intQuotientWholeNbr As Integer = Math.Floor(x / y)
Dim intQuotientRemainder As Single = x Mod y

MsgBox(String.Format("{0} divided by {1} is {2} with a remainder of {3}.", x, y, intQuotientWholeNbr, intQuotientRemainder))
End Sub

End Module
``````
0
Chief Technology OfficerCommented:
Or simply leave as a Decimal data type instead of Single which loses precision from the original entered Decimal.  Doesn't affect the fact that the logic is simple to get the two values...

Therefore, you should be able to get either to work VinnyG3, but let us know if not working for you still...
0
Author Commented:
Ok i tried this code above and first off i am getting an error saying that 'first' is not a member of 'string'. Also my answer is still coming out as a decimal.
0
Chief Technology OfficerCommented:
For first issue, think I left out the ToString:

Label1.Text.ToString.Trim.First ==> should return first char -- this is not required, you can just do this too

Dim op As String = Label1.Text

The point was I would take in your inputs and then will make calculations easier without having to keep going back the UI field values allowing you to do some data cleansing without having to do it repeatedly.

For the second, do you mean that it is being displayed as 7.0 instead of 7.

Again I was just making a point that you can simplify the code logic since you had commonality and so resultA is set as a Decimal so you may be seeing that.  Please let me know what you mean there.
0
DeveloperCommented:
Hello, All,

Yes, I somehow had the impression that we were doing integer arithmetic here.  If that's not the case, a more sophisticated solution will be required.

Cheers,
Randy
0
Chief Technology OfficerCommented:
I think I have .NET 3.5 on this machine too, so I may be seeing members of the String class you are not (on the .First issue).
0
Chief Technology OfficerCommented:
That was my impression too, but sounds like it is not. :)
0
Chief Technology OfficerCommented:
This still works though in .NET 3.5, so maybe I am seeing improvements if not working same for you:

Dim x As Decimal = 103.034
Dim y As Decimal = 13.25

Dim intQuotientWholeNbr As Integer = Math.Floor(x / y)
Dim intQuotientRemainder As Decimal = x Mod y

MsgBox(String.Format("{0} divided by {1} is {2} with a remainder of {3}.", x, y, intQuotientWholeNbr, intQuotientRemainder))
0
Commented:
mwvisa:
Or simply leave as a Decimal data type instead of Single which loses precision from the original entered Decimal.  Doesn't affect the fact that the logic is simple to get the two values...

I understand, I wasnt making any beef about your code. I was trying to impress upon the author that, for a calculator, losing that much precision is probably not a good thing.
VinnyG3:
Ok i tried this code above and first off i am getting an error saying that 'first' is not a member of 'string'. Change: Dim op As Char = Label1.Text.Trim.First()
To: Dim op As Char = Trim(Label1.Text)
The Char object will automatically take the first character of the returned string.
Also my answer is still coming out as a decimal.
What type are you shooting for? If there is a particular way you need it displayed, do tell.
0
Author Commented:
Ok that helps out a lot. To answer your question about how i would like it displayed, 29 / 9 = 3 with a remainder of 2. As of right no it shows up as 3 with a remainder of 0.22222222222.....

Thank you

Tyler
0
Commented:
Then you could use mwvisa's first example. Just be advised that standard rounding rules will apply (<=.5 rounds down, >=.51 rounds up).

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Ok i might just be ignorant about this but im still not seeing where to put that into the code that i have below.

Thanks

Tyler
``````Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim op As Char = Trim(Label1.Text)
Dim x, y, resultA, resultB As Decimal
Dim showResultB As Boolean = False

Decimal.TryParse(TxtBox1.Text, x)
Decimal.TryParse(TxtBox2.Text, y)

If op = "+" Then
resultA = x + y
End If
If op = "-" Then
resultA = x - y
End If
If op = "*" Then
resultA = x * y
End If
If op = "/" Then
resultA = Math.Floor(x / y)
resultB = (x / y) - resultA
showResultB = True
End If

Lbl3.Text = resultA
If showResultB Then LblR.Text = resultB
End Sub
``````
0
Chief Technology OfficerCommented:
This code here:
resultB = (x / y) - resultA

You could just replace with original suggestion to do MOD.
resultB = x MOD y

This code is under If op = "/" Then section.

If the remainder is a decimal like 2.1 it will show as such, but you can change that by changing Dim resultB AS Decimal to Dim resultB AS Integer noting all the things that ladarling is pointing out on precision and rounding.
0
Author Commented:
Thanks guys this helped out a lot
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.