This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

thank you

Tyler

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.

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

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
```

Hello, VinnyG3,

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

Cheers,

Randy

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

Cheers,

Randy

Really? Replace 13 with 13.25 and see if that rounded number seems like good calculator accuracy (7 with a remainder 7).

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
```

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
```

Therefore, you should be able to get either to work VinnyG3, but let us know if not working for you still...

Label1.Text.ToString.Trim.

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.

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

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))

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.

To: Dim op As Char = Trim(Label1.Text)

The Char object will automatically take the first character of the returned string.

What type are you shooting for? If there is a particular way you need it displayed, do tell.

Thank you

Tyler

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trialThanks

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
```

resultB = (x / y) - resultA

I had used ladarling's suggestion.

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.

.NET Programming

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

+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.

Open in new window