Results 1 to 7 of 7

Thread: Finding the value of the nth digit of a number

  1. #1

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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:
    1. Private Sub Command1_Click()
    2.  
    3.     'Text1 is the number you would like searched
    4.     'Text2 is the digit you are looking for
    5.     'Command1.... do I need to comment on that?
    6.     'Label1 tells you what the answer is
    7.      
    8.    
    9.     'The problem is simple... Given any number, what is the value of the nth
    10.     'digit from the right? eg x=45364, n=3. What is the value of the third digit
    11.     'from the right? Obviously, by looking at it, it's 3. But if for some odd
    12.     'reason you can't see the number, this program will figure it out for you.
    13.     'Cheers!
    14.  
    15.     Dim x As Long
    16.     Dim s As Integer
    17.     Dim i As Integer
    18.     Dim counter As Integer
    19.      
    20.     s = Len(Text1.Text) 'number of digits in the number
    21.     n = Val(Text2.Text) 'position from the right you want to know
    22.     x = Text1.Text 'your original number
    23.      
    24.     For counter = s To n + 1 Step -1
    25.         x = x - (Int((x / (10 ^ (counter - 1)))) * (10 ^ (counter - 1)))
    26.     Next counter 'presto chango... all numbers in front of your digit are removed
    27.      
    28.     x = Int(x / (10 ^ (n - 1))) 'abra kadabra, all numbers behind yours are gone
    29.      
    30.     Label1.Caption = x 'and the magic number mystically appears in the label
    31.      
    32. End Sub
    Last edited by timeshifter; Dec 9th, 2005 at 09:25 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: EyeRmonkey, this is all for you

    Mid$() ?

    Or do you not like cheating that way?

  3. #3

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: EyeRmonkey, this is all for you

    needed equations... VB logic didn't work for the situation,

  4. #4
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    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:
    1. int(x / 10 ^ i) - int(x / 10 ^ (i +1)) * 10
    Or it could be written better like this:
    VB Code:
    1. (x \ 10 ^ i) - (x \ 10 ^ (i +1)) * 10
    Attached Images Attached Images  
    Last edited by eyeRmonkey; Dec 9th, 2005 at 09:08 PM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5

    Thread Starter
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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:
    1. Private Sub Command1_Click()
    2.  
    3.    Dim x As Long
    4.    Dim n As Integer
    5.  
    6.    n = Val(Text2.Text) 'position from the right you want to know
    7.    x = Text1.Text 'your original number
    8.  
    9.    x = Int((x - (Int((x / (10 ^ n))) * (10 ^ n))) / (10 ^ (n - 1)))
    10.    Label1.Caption = x
    11.  
    12. End Sub

  6. #6
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    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
    Attached Images Attached Images  
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    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.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

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



Click Here to Expand Forum to Full Width