Finding the value of the nth digit of a number
EyeRmonkey,I told you it couldn't be done in a single formula(which is a dead lie in hindsight...), simply because the number of operations you have to do changes depending on the length of the number and what digit you're looking for(not really...i was dumb when i wrote this post).
If you want the code I came up with, here it is for ya. Fairly simple, actually. Enjoy!
VB Code:
Private Sub Command1_Click()
'Text1 is the number you would like searched
'Text2 is the digit you are looking for
'Command1.... do I need to comment on that?
'Label1 tells you what the answer is
'The problem is simple... Given any number, what is the value of the nth
'digit from the right? eg x=45364, n=3. What is the value of the third digit
'from the right? Obviously, by looking at it, it's 3. But if for some odd
'reason you can't see the number, this program will figure it out for you.
'Cheers!
Dim x As Long
Dim s As Integer
Dim i As Integer
Dim counter As Integer
s = Len(Text1.Text) 'number of digits in the number
n = Val(Text2.Text) 'position from the right you want to know
x = Text1.Text 'your original number
For counter = s To n + 1 Step -1
x = x - (Int((x / (10 ^ (counter - 1)))) * (10 ^ (counter - 1)))
Next counter 'presto chango... all numbers in front of your digit are removed
x = Int(x / (10 ^ (n - 1))) 'abra kadabra, all numbers behind yours are gone
Label1.Caption = x 'and the magic number mystically appears in the label
End Sub
Re: EyeRmonkey, this is all for you
Mid$() ?
Or do you not like cheating that way? :p
Re: EyeRmonkey, this is all for you
needed equations... VB logic didn't work for the situation,
1 Attachment(s)
Re: EyeRmonkey, this is all for you
Hehe. I figured it out after I signed off last night (thanks to VBs immediate window pane).
I'll post an explaination of it later, but if you have a math-brain (;)) you can probably figure it out.
(See attached image)
x = any number (IE: 967,852)
i = number of positions from the right (IE: 4 positions from the right. AKA - the 1,000s place)
the number the function outputs should be the value at position I. (IE: 7)
... In VB it would look like this:
VB Code:
int(x / 10 ^ i) - int(x / 10 ^ (i +1)) * 10
Or it could be written better like this:
VB Code:
(x \ 10 ^ i) - (x \ 10 ^ (i +1)) * 10
Re: EyeRmonkey, this is all for you
Ok, I severely lied in my first post... here's the VB version of that equation.. give or take...
VB Code:
Private Sub Command1_Click()
Dim x As Long
Dim n As Integer
n = Val(Text2.Text) 'position from the right you want to know
x = Text1.Text 'your original number
x = Int((x - (Int((x / (10 ^ n))) * (10 ^ n))) / (10 ^ (n - 1)))
Label1.Caption = x
End Sub
1 Attachment(s)
Re: Finding the value of the nth digit of a number
The reason I did this was to find a way (all by my lonesome) to convert binary to decimal with only a formula. Here is the full thing.
n = is the length of the binary number
Re: Finding the value of the nth digit of a number
Does anyone know of a way to remove the sigma notation to simplify the function I posted above?
I know that Sigma(i) = n(n+1)/2, so I was hoping for a way to simplify the 10 ^ i part.