Way to get number of digits in an int?

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

    Easy recursive way

    int    get_int_lenght(current_lenght, value)
    {
     if (value / 10 < 10)
        return (current_lenght + 1);
    return (get_int_lenght(current_lenght + 1, value))
    }
    

    not tested

提交回复
热议问题