Way to get number of digits in an int?

后端 未结 30 1257
梦毁少年i
梦毁少年i 2020-11-22 17:21

Is there a neater way for getting the number of digits in an int than this method?

int numDigits = String.valueOf(1000).length();
30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 17:22

    Try converting the int to a string and then get the length of the string. That should get the length of the int.

    public static int intLength(int num){
        String n = Integer.toString(num);
        int newNum = n.length();
        return newNum;
    }
    

提交回复
热议问题