Finding a Specific Digit of a Number

后端 未结 9 1889
深忆病人
深忆病人 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:19

    A direct answer is:

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

    You should include the <math> library

    0 讨论(0)
  • 2020-12-17 00:21
    const char digit = '0' + number.at(n);
    

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

    0 讨论(0)
  • 2020-12-17 00:22
    number = 123456789
    n = 5
    
    tmp1 = (int)(number / 10^n);   // tmp1 = 12345
    tmp2 = ((int)(tmp1/10))*10;    // tmp2 = 12340
    digit = tmp1 - tmp2;           // digit = 5
    
    0 讨论(0)
提交回复
热议问题