How to get the separate digits of an int number?

前端 未结 30 2354
陌清茗
陌清茗 2020-11-22 03:03

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0.

How can I get

30条回答
  •  无人共我
    2020-11-22 03:24

    Try this:

    int num= 4321
    int first  =  num % 10;
    int second =  ( num - first ) % 100 / 10;
    int third  =  ( num - first - second ) % 1000 / 100;
    int fourth =  ( num - first - second - third ) % 10000 / 1000;
    

    You will get first = 1, second = 2, third = 3 and fourth = 4 ....

提交回复
热议问题