tags:

views:

603

answers:

6

I am a newbie to programming and need some help with the basics.

I have a function which takes in an integer value. I want to be able to grab the first digit (or the first and second digits in some cases) of this integer and do something with it.

What is the best way in VB.NET to get the first digit of an integer (or the first and second)?

+1  A: 

If you need the digits starting from the end of the integer, just get the modulu result for the tens or the hundreds, according to how many digits you need.

Dim n As Integer
n Mod 10

for the first digit, or:

n Mod 100

for the second and first digits.

If you need the first and second digits from the beginning of the number, there is another answer here which will probably help you.

Yuval A
I guess that depends on your definition of *first* digit :)
Thorarin
this returns the last digit(s), not the first
CodeByMoonlight
+3  A: 

I'm not well versed in VB syntax, so forgive me for the syntax errors:

dim i as integer
while i >= 10
    i = i \ 10
end while
msgbox "i = " & i

Note, this prints the "first from the left" digit. Like, for "12345" it would print "1".

Vilx-
And for 107, it would print 10 which is not *quite* right. Let me fix that for you :-)
paxdiablo
Indeed, thank you! :)
Vilx-
+1  A: 

Hi there.

To Yuval, are you sure your answer is correct? I just tried this:

Module Module1

    Sub Main()
        Dim i As Integer = 34

        Dim k As Integer = i Mod 10

    End Sub

End Module

and the result of k is 4, not 3. The Mod operator rounded up the number.

Jason Evans
Check out this post: http://stackoverflow.com/questions/701322/how-can-you-get-the-first-digit-in-an-int-c/701355
Jason Evans
It looks like Vilx- was on the right path.
Jason Evans
Yuval's code prints the first digit from the right, not the left. Hence the 4.
Thorarin
Ahh, cheers for explaining that - I wasn't sure how the 4 was being generated.
Jason Evans
+5  A: 
firstDigit = number.ToString().Substring(0,1)

firstTwoDigits = number.ToString().Substring(0,2);

int.Parse(firstDigit)

int.Parse(firstTwoDigits)

and so forth

Xian
+1 That's the answer I was about to post...
PhilPursglove
Ever heard of negative numbers? :)
MusiGenesis
use Math.Abs(number).ToString if there are such cases
mangokun
Thanks that was what I was after. Noted the issue with the negative number but it is not a problem in this case.
Cunners
+1  A: 

for first digit you can use:

  Dim number As Integer = 234734
  Dim first = number.ToString.ToCharArray()(0)

for second digit you can use:

 Dim number As Integer = 234734
 Dim second = number.ToString.ToCharArray()(1)
Wael Dalloul
+1  A: 

This would work. You can use Math.ABS, absolute value, to eliminate negative. The number from left could be replaced by a function if you are using logic, like the overall length of the number, to determine how many of the leading characters you are going to use.

Dim number As Integer = -107
Dim result As String
Dim numberFromLeft As Integer = 2

result = Math.Abs(number).ToString.Substring(0, numberFromLeft)

This results in 10 it is a string but converting it back to a number is easy if you need to. If you need to keep track if it was positive or negative you could use the original value to apply that back to you parsed string.

CertifiedCrazy