Way to get number of digits in an int?

后端 未结 30 1258
梦毁少年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:39

    Enter the number and create an Arraylist, and the while loop will record all the digits into the Arraylist. Then we can take out the size of array, which will be the length of the integer value you entered.

    ArrayList a=new ArrayList<>();
    
    while(number > 0) 
    { 
        remainder = num % 10; 
        a.add(remainder);
        number = number / 10; 
    } 
    
    int m=a.size();
    

提交回复
热议问题