Example. 123456, and we want the third from the right (\'4\') out.
The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1).
C/C++/C# preferred.
Just for fun, here is the C# extension class for it:
public static class IntExtensions
{
///
/// Returns the nth digit from an int,
/// where 0 is the least significant digit
/// and n is the most significant digit.
///
public static int GetDigit(this int number, int digit)
{
for (int i = 0; i < digit; i++)
{
number /= 10;
}
return number % 10;
}
}
Usage:
int myNumber = 12345;
int five = myNumber.GetDigit(0);
int four = myNumber.GetDigit(1);
int three = myNumber.GetDigit(2);
int two = myNumber.GetDigit(3);
int one = myNumber.GetDigit(4);
int zero = myNumber.GetDigit(5);