QUOTE(NickDMax @ 18 Jul, 2008 - 09:01 PM)

to extract a given digit:
take the number and divide it by 10^(digit -1), then find result mod 10.
You need to do a couple of checks:
First off, if digit == 1 then 10^(digit-1) == 0 so you would divide by zero (bad -- end of the universe and all). So for this you need a seperate calculation of finding the mod 10 of the number.
Secondly, barring the above digit should be between 2 and log10(number) + 1 (the total number of digits). The upper limit is not really all that important since it will just generate a zero, though if digit gets too large then you will cause an overflow.
Thanks, that seems to work, except its a sort of "off by one error." For example, I have the arbitrary number 546721. When I ask for the 1000s digit I get 4.
CODE
result=(number / ( 10^(digit-1) ) )%10;
Also, there is no problem with dividing by zero, because if the digit is one, you get ten to the "zeroth" power. Anything to the zeroth power is one, so there would be no problem with that. Or does it happen to work differently in C++?
This post has been edited by seuzy13: 19 Jul, 2008 - 08:20 AM