1

I'm making a simple calculator code, and when I do the division I want it to show not just the whole number but the decimal number. This is my division code:

 get1.Text = Int(mygive.Text \ rate.Text)

I've also tried:

get1.Text = Int(mygive.Text / rate.Text)

I want it to show numbers like this: 2060.0891

Thanks in advance!

4 Answers 4

3

You need to convert the numbers to Double prior to doing the division. CDbl is an appropriate Type Conversion Function for this:

Dim answer = CDbl(mygive.Text) / CDbl(rate.Text)
get1.Text = answer.ToString()
0
1

Your Int is converting everything to whole numbers. Try converting to double.

0
0

It's showing only the whole part of the number because you're declaring an Integer, which is insufficient, as it can only store whole numbers.

You need to use the Double data type.

Also, you're attempting to apply arithmetic operations on Strings, which is invalid. You need to convert those strings to be of the type Double before you can do / on them.

1
  • VB will convert string to number and back by yourself. So only if converting not possible will throw an error.
    – Fabio
    Mar 24, 2013 at 0:08
0

Use converting to Double/Decimal

with VB help function:

get1.Text = CDec(mygive.Text / rate.Text).ToString()

with .NET function

get1.Text = Convert.ToDecimal(mygive.Text / rate.Text).ToString()
3
  • 1
    Please explain what is the expected output of the operator / when applied to two strings
    – Steve
    Mar 24, 2013 at 0:08
  • @Steve, VB automatically convert string to numbers if code expecting this(using Help Convertion functions (CStr, CInt, CDbl,CDec)
    – Fabio
    Mar 24, 2013 at 0:13
  • This is true only if you disable OPTION STRICT ON. And this is really a disaster waiting to happen. codinghorror.com/blog/2005/08/…
    – Steve
    Mar 24, 2013 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.