# Thread: Why is this code rounding the result?

1. Registered User
Join Date
Jul 2005
Location
Burlington, VT
Posts
181

## Why is this code rounding the result?

I have this code, element is a large array with many items with decimals but no matter what I do the answer is getting rounded to an integer. Any idea why?

Code:
```Dim quantity1 As Double = Int(Quant1.Text)
Dim quantity2 As Double = Int(Quant2.Text)
Dim quantity3 As Double = Int(Quant3.Text)
Dim amu1 As Double = elements(el1.SelectedIndex + 1)
Dim amu2 As Double = elements(el2.SelectedIndex + 1)
Dim amu3 As Double = elements(el3.SelectedIndex + 1)
Dim mass1 As Double = quantity1 * amu1
Dim mass2 As Double = quantity2 * amu2
Dim mass3 As Double = quantity3 * amu3
result1.Text = mass1
result2.Text = mass2
result3.Text = mass3```

2. If you put a breakpoint at the "result1.Text = mass1" line, what are the values of "quantity1", "amu1" and "mass1"? If the program is allowed to run again, what is the final value in the result1 textbox?

3. Registered User
Join Date
Jul 2005
Location
Burlington, VT
Posts
181
Ok, the problem is the selection of the element via the combo box. I still don't know why it is doing it though. I used the exact same code in an earlier sub and it worked fine.

4. SelectedIndex method of combo box returns a whole number, if you are trying to select a fraction such as: 1.25 then try adding the fractional data to the elements ItemData and select that instead of the index number.

Also, any fractions entered in text boxes such as in: Dim quantity1 As Double = Int(Quant1.Text) are being rounded due to Int() function. You are Dimming it as a Double so I assume you are entering fractions? If so use CDbl() instead of Int() to preserve your fractional amounts.

Maybe I am following what you are trying to do, maybe not... I dont know what this elements() function is so...

anyway hope it helps.

5. Registered User
Join Date
Jul 2005
Location
Burlington, VT
Posts
181
The idea is the index selected on the combo box then selects that index in an array called elements.

6. I see, How are you populating your element with data?

7. Registered User
Join Date
Jul 2005
Location
Burlington, VT
Posts
181
Heres the example of how I did it.

Code:
```dim elements(10) as double

elements(0)=1.1
elements(1)=2.2```

8. Why are you adding "1" to SelectedIndex? SelectedIndex is 0-based - the same as the array indexing.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•