Splitting an integer number into digits?

Discussion in 'C' started by nigelmercier, Sep 11, 2010.

  1. nigelmercier

    nigelmercier New Member

    Sep 11, 2010
    Likes Received:
    Trophy Points:
    I'm working on a PIC project, and my brain has almost exploded! I'm hoping that by writing down my problem, that the answer will come to me, but please feel free to help!

    Background (mostly irrelevant to an outsider): I'm storing a number in a variable (say r, an unsigned integer) which represents the ratio of a pulley system. The ratio itself is a real number and unlikely to be an integer, but I need to store it as an integer for storage reasons (not relevant to this discussion). When used, the integer will be divided by a constant (probably 4000) and the result will be used via a cast to a float.

    In order for the user to set the value of the integer (using select and increment buttons), I have to first display it using 3 single variables (d1, d10, d100) that represent each digit up to 1000, then the thousands (and possible tens of thousands) as another variable (d1000).

    For example, r = 32768 is comprised of:

    d1000 = 32
    d100 = 7
    d10 = 6
    d1 = 8

    Getting d1000 is easy, it's just r/1000. For d100 I considered (r-(1000*d1000))/100 which is OK, but it gets messy after that. I'm guessing there is a way of doing this with remainders, but my brain is too hot.

    [Later] Well, it didn't work, I'm still stuck. Anyone care to help?


    PS I'm over 50, this is not homework!
  2. techgeek.in

    techgeek.in New Member

    Dec 20, 2009
    Likes Received:
    Trophy Points:
    EOC (exploitation of computers)..i m a Terminator.
    Not an alien!! for sure
    Home Page:
    do like this:-

    d1000= r/1000
    d100= (r/100)%10

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice