Split a integer into its separate digits

后端 未结 3 1065
生来不讨喜
生来不讨喜 2020-12-30 16:05

Say I have an integer, 9802, is there a way I can split that value in the four individual digits : 9, 8, 0 & 2 ?

3条回答
  •  死守一世寂寞
    2020-12-30 16:49

    You probably want to use mod and divide to get these digits.

    Something like:

    Grab first digit:
    
       Parse digit: 9802 mod 10 = 2
       Remove digit: (int)(9802 / 10) = 980
    
    Grab second digit:
    
       Parse digit: 980 mod 10 = 0
       Remove digit: (int)(980 / 10) = 98
    

    Something like that.

提交回复
热议问题