Sign in to follow this  
Simplicity

Digital Value Detection

Recommended Posts

Given an integer say, 131345, how to detect if a digit is a specific value? For example, how to test to see if the unit digit of 131345 is a 5 or not? What about for floating points? [Edited by - Simplicity on October 23, 2005 9:02:26 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to test for the remainder of the integer division.

For your example, you need the value of the unit digit, so you have to compute the remainder of the division by 10:

131345 % 10 = 5

If you for example the value of the hunderds you would need to do it as follows:
First compute the remainder of the division by 1000 and then compute the integer division by 100:

131345 % 1000 = 345
345 / 100 = 3

Share this post


Link to post
Share on other sites
The most obvious answer would be something like:

int digit = (n / 10000) % 10;

That would be for the 5th digit from the right. For the nth digit:

int digit = number;
for (int i = 0; i < digit_index; ++i)
digit /= 10;
digit %= 10;

Share this post


Link to post
Share on other sites
This is probably a better way, unless you're testing a specific digit every time, then you could just use the constant division/modulous method. Using a for loop is a rather unnecessary waste of processor cycles.


bool TestDigit(int n, int digit, int value) {
return ((n / (int)pow(10,digit - 1)) % 10 == value)
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Simplicity
What about for floating points where the molodus is unavailable? I guess we would convert it to integer first?


Well, one option is to add this to the beginning:


int AdjustFloat(float n) {
while (fabs(n-(int)n) > ERROR_AMT)
n *= 10;
return (int)n;
}


And you'd call it like this:

#define ERROR_AMT 0.00001
int digit = 2
float fval = 1.025;
TestDigit(AdjustFloat(fval),digit,2); // This should return true

Share this post


Link to post
Share on other sites
You could avoid half the math by just getting a string from the number.


float x = 131345.55656;
stringstream cnv;
string s;
cnv << x; //convert the number
cnv >> s; //Get you string

size_t pos = find ( s.begin(), s.end(), '.' ); //Find the decimal point.

//now to get the 100's place, 10^2 == 100, so move to the left 2 from the decimal
cout << s[pos-2];



You may need to check that pos is NOT s.npos, just in case there is no decimal point. You should also bounds check to make sure you are not reading outside the string.

Share this post


Link to post
Share on other sites
Quote:
Original post by Simplicity
What about for floating points where the molodus is unavailable? I guess we would convert it to integer first?


On the contrary, it is available. You could do this:

value = (int)fmod(num * pow(10.0, -digit), 10.0);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this