How to get the separate digits of an int number?

前端 未结 30 1939
陌清茗
陌清茗 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:32

    Integer.toString(1100) gives you the integer as a string. Integer.toString(1100).getBytes() to get an array of bytes of the individual digits.

    Edit:

    You can convert the character digits into numeric digits, thus:

      String string = Integer.toString(1234);
      int[] digits = new int[string.length()];
    
      for(int i = 0; i

提交回复
热议问题