:
:I am using the following code to pick off each digit of a number,
:from right to left. The number I am working with is 84357. So for
:the first iteration it should return the number 7 and for the second
:iteration it should return the number 5, and so on. But for some
:reason on the first iteration returns the expected results. Each
:subsequent iteration returns the number plus 1. In order words, when
:I run the program I am getting: 7, 6, 4, and 9. Instead of 7, 5, 3,
:4, and 8. Can someone tell me what's wrong with my code?
:
: Sub Main()
: Dim input As Integer = 84357
: Dim digit As Integer
: Dim decimalNumber As Integer
: Dim divisor As Integer = 1
:
:
:
: While input >= 0
:
: digit = (input / divisor) Mod 10
:
: divisor *= 10
:
: End While
:
: End Sub ' Main
The root problem is you are dividing by 10, creating a floating point
value in the process, then implicitly coverting it back to an integer.
Implicit conversions are generally a bad idea and your sample code
here is a good example of this. The "Option Strict" statement was put
into place to address that and in my opinion it significantly improved
the VB language. I strongly recommend you use it unless you have a
good reason not to.
Here is a simpler bit of code you can use to achieve your desired
result:
'Option Strict means you must declare your variables by name and
'type before you can use it. This is a good idea in almost all cases
Option Strict
Public Module [module]
Sub Main()
'this sample only needs one variable because we will be modifying
'it with each pass and working on the resulting value
Dim input As Integer = 84357
'your original code never modified the value of "input" and
'so it was always ">= 0". This would have resulted in an
'endless loop. Here, we are testing for any value greater
'than 0. Since we reduce the value of "input" by 10 in each
'pass, we eventually reduce it to 0 and thereby exit the loop
While input > 0
'we'll use the MOD function you originally had as it served
'your purpose quite well
System.Console.WriteLine(input Mod 10)
'now we'll do "Integer" division (using the back slash
'operator instead of the forward slash). This returns an
'integer value instead of a floating point. For example,
'compare these results:
'
' 84357 \ 10 = 8435
' 84357 / 10 = 8435.7
'
'In your original code, you got back a floating point (single)
'value which you then performed the MOD operation on. In that
'case, you generated the following values behind the scenes:
'
' 84357 / 10 = 8435.7
' 8435.7 MOD 10 = 5.70000000000073
'
'This final value (5.70000000000073) was then changed back to
'an integer value behind the scenes by the compiler (that is,
'implicitly) in order to store it in the 'digit' integer
'variable. Such an implicit conversion automatically invokes
'the built in rounding behavior of VB which rounds 5.7 up
'to 6. This was compounded with each pass.
'
'In contrast, by integer dividing by 10 as shown below, we
'effectively strip off the trailing digit with each pass without
'the implicit conversion or the rounding. Integer division is
'very efficient and can be used whenever you specifically don't
'need or want to retain the digits to the right of the decimal
'point after you've completed the division operation.
Input = input \ 10
End While
End Sub
End Module
Ralf
--
AA #2250
-------------------------------------------------------------
* ^~^ ^~^ *
* _ {| |} {| |} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
-------------------------------------------------------------
Nature is the canvas of creation and evolution but one of
the brushes. Religion points to the mind of God; Science
reveals its unfolding. The subjective apprehends knowledge
while the objective facilitates understanding.
In all things, yin and yang - ever flowing, one into the
other; always overtaking, always overtaken.