C++ get each digit in int

前端 未结 13 1294
谎友^
谎友^ 2020-12-02 21:50

I have an integer:

int iNums = 12476;

And now I want to get each digit from iNums as integer. Something like:

foreach(iNum          


        
13条回答
  •  独厮守ぢ
    2020-12-02 22:05

    The answer I've used is this simple function:

    int getDigit(int n, int position) {
    
        return (n%(int)pow(10, position) - (n % (int)pow(10, position-1))) / (int)pow(10, position-1);
    
    }
    

    Hope someone finds this helpful!

提交回复
热议问题