429,116 Members | 1,509 Online
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

# C: Splitting an integer number into digits?

 P: 45 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? Nigel PS I'm over 50, this is not homework! Sep 11 '10 #1