Finding a Specific Digit of a Number

后端 未结 9 1949
深忆病人
深忆病人 2020-12-16 23:32

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...

9条回答
  •  忘掉有多难
    2020-12-17 00:12

    A more general approach:

    template
    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.).

提交回复
热议问题