Solved

# division in VB

Posted on 2008-11-07
254 Views
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
0
Question by:VinnyG3

LVL 59

Expert Comment

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 Comment

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

LVL 11

Expert Comment

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

LVL 12

Expert Comment

Hello, VinnyG3,

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

Cheers,
Randy
0

LVL 11

Expert Comment

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

LVL 59

Expert Comment

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

LVL 59

Expert Comment

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

LVL 59

Expert Comment

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 Comment

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

LVL 59

Expert Comment

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

LVL 12

Expert Comment

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

LVL 59

Expert Comment

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

LVL 59

Expert Comment

That was my impression too, but sounds like it is not. :)
0

LVL 59

Expert Comment

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

LVL 11

Expert Comment

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 Comment

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

LVL 11

Accepted Solution

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

0

Author Comment

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

LVL 59

Assisted Solution

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 Closing Comment

Thanks guys this helped out a lot
0

## Featured Post

Wouldnâ€™t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldnâ€™t it be good if you could write code like this? (CODE) In .NET 3.5, this is possibleâ€¦
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct â€¦
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the fileâ€¦
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.