Not able to get rid of remainders

CynicWolf

New member
Joined
Feb 6, 2012
Messages
1
Programming Experience
Beginner
Hello, I'm in a Visual Basic 2010 class and I haven't had any major road blocks until now.
I'm trying to get rid of a decimal place on a user input text box.

My Program Runs just fine but it will round the number of years up instead of dropping the remainder, and with my understanding the integer division should have kicked the remainder out?
Here is the important part of my code, and I know that i don't know anything but any help would be great.
(Also I did ask my instructor and I think is was a case of "If you cant help 'em baffle 'em")

'Declarations
Dim intYear As Integer
Dim strYear As String
Dim dblPrinciple As Double
Dim intLoops As Integer
Dim dblRate As Double
Dim dblAmount As Double

'Checks data
If (txtYears.Text <> "") AndAlso IsNumeric(txtYears.Text) AndAlso (txtYears.Text >= 1) Then
intYear = Convert.ToString(txtYears.Text) '<the problem is between here
intYear = intYear \ 1
intLoops = Convert.ToInt32(intYear) '< and here....(I think)
If (txtPrincipal.Text <> "") AndAlso IsNumeric(txtPrincipal.Text) AndAlso (txtPrincipal.Text > 0) Then
dblPrinciple = Convert.ToDouble(txtPrincipal.Text)
If (txtRate.Text > "") AndAlso IsNumeric(txtRate.Text) AndAlso (txtRate.Text > 0) Then
dblRate = Convert.ToDouble(txtRate.Text)

'Calculates and displays each year
For intYear = 1 To intLoops
dblPrinciple = dblPrinciple * (1 + dblRate)
dblAmount = dblPrinciple.ToString("C2")
lstDisplay.Items.Add("Year: " & intYear & " $" & dblAmount)
Next
 
Does this make sense?
intYear = Convert.ToString(txtYears.Text)
You have the Text of a TextBox, which is a String, you call Convert.ToString, which converts your String to a String (???) and then you assign that String to an Integer variable?

Does this make sense?
intYear = intYear \ 1
The whole point of integer division is to divide one Integer by another Integer and get the whole number part of the result. How is dividing a whole number by 1 going to produce a fractional part? Integer division can only be performed on Integers so it's no use for removing the fractional part of a decimal number.

You need to get the steps right first, before you can write code to implement them. First, you need to convert the String from the TextBox to a Double or Decimal. For that, you should be using the TryParse method of the appropriate type. Next, you use Math.Truncate to drop the fractional part of the number. Finally, you need to convert that Double or Decimal to an Integer.
 
Back
Top