# 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 on other sites
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 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 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 on other sites
What about for floating points where the molodus is unavailable? I guess we would convert it to integer first?

##### Share on other sites
Quote:
 Original post by SimplicityWhat 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.00001int digit = 2float fval = 1.025;TestDigit(AdjustFloat(fval),digit,2); // This should return true

##### 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 numbercnv >> s; //Get you stringsize_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 decimalcout << 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 on other sites
Quote:
 Original post by SimplicityWhat 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 on other sites
For the converting to string method, considering performance, does it have more overhead compared to the other methods?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628306
• Total Posts
2981942

• 9
• 11
• 11
• 11
• 10