Way to get number of digits in an int?

后端 未结 30 1270
梦毁少年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条回答
  •  萌比男神i
    2020-11-22 17:24

    One wants to do this mostly because he/she wants to "present" it, which mostly mean it finally needs to be "toString-ed" (or transformed in another way) explicitly or implicitly anyway; before it can be presented (printed for example).

    If that is the case then just try to make the necessary "toString" explicit and count the bits.

提交回复
热议问题