Finding a Specific Digit of a Number
Asked Answered
Z

10

8

I'm trying to find the nth digit of an integer of an arbitrary length. I was going to convert the integer to a string and use the character at index n...

char Digit = itoa(Number).at(n);

...But then I realized the itoa function isn't standard. Is there any other way to do this?

Zajac answered 10/12, 2010 at 15:55 Comment(1)
sprintf? (C rather than C++, but it wil work :))Aspersion
K
21

(number/intPower(10, n))%10

just define the function intPower.

Keble answered 10/12, 2010 at 15:58 Comment(0)
P
4

You can also use the % operator and / for integer division in a loop. (Given integer n >= 0, n % 10 gives the units digit, and n / 10 chops off the units digit.)

Promissory answered 10/12, 2010 at 16:1 Comment(0)
B
1

Itoa is in stdlib.h.

You can also use an alternative itoa:
Alternative to itoa() for converting integer to string C++?
or
ANSI C, integer to string without variadic functions

Belmonte answered 10/12, 2010 at 15:58 Comment(0)
T
1
number = 123456789
n = 5

tmp1 = (int)(number / 10^n);   // tmp1 = 12345
tmp2 = ((int)(tmp1/10))*10;    // tmp2 = 12340
digit = tmp1 - tmp2;           // digit = 5
Thom answered 10/12, 2010 at 16:2 Comment(1)
Note that ^ is bitlevel XOR in C++.Keble
S
1

You can use ostringstream to convert to a text string, but a function along the lines of:

char nthDigit(unsigned v, int n)
{
    while ( n > 0 ) {
        v /= 10;
        -- n;
    }
    return "0123456789"[v % 10];
}

should do the trick with a lot less complications. (For starters, it handles the case where n is greater than the number of digits correctly.)

-- James Kanze

Sixteenmo answered 10/12, 2010 at 16:6 Comment(0)
L
1

It is also possible to avoid conversion to string by means of the function log10, int cmath, which returns the 10th-base logarithm of a number (roughly its length if it were a string):

unsigned int getIntLength(int x)
{
    if ( x == 0 )
            return 1;
    else    return std::log10( std::abs( x ) ) +1;
}

char getCharFromInt(int n, int x)
{
    char toret = 0;
    x = std::abs( x );
    n = getIntLength( x ) - n -1;

    for(; n >= 0; --n) {
        toret = x % 10;
        x /= 10;
    }

    return '0' + toret;
}

I have tested it, and works perfectly well (negative numbers are a special case). Also, it must be taken into account that, in order to find tthe nth element, you have to "walk" backwards in the loop, subtracting from the total int length.

Hope this helps.

Lexical answered 10/12, 2010 at 16:25 Comment(0)
S
1

A direct answer is:

char Digit = 48 + ((int)(Number/pow(10,N)) % 10 );

You should include the <math> library

Symbol answered 10/12, 2010 at 16:51 Comment(0)
C
0
const char digit = '0' + number.at(n);

Assuming number.at(n) returns a decimal digit in the range 0...9, that is.

Camiecamila answered 10/12, 2010 at 15:57 Comment(1)
I can't convert number to a string to use .at(n), though. itoa isn't standard.Zajac
B
0

A more general approach:

template<int base>
int nth_digit(int value, int digit)
{
    return (value / (int)pow((double)base, digit)) % base;
}

Just lets you do the same thing for different base numbers (e.g. 16, 32, 64, etc.).

Bystander answered 10/12, 2010 at 18:16 Comment(0)
G
0

An alternative to itoa is the std::to_string method. So, you could simply do:

char digit = to_string(number)[index]
Gerhardine answered 14/8, 2021 at 8:31 Comment(1)
Need to handle out of bounds.Blucher

© 2022 - 2024 — McMap. All rights reserved.