find the “string length” of an int

前端 未结 6 1916
情话喂你
情话喂你 2020-12-31 21:13

basically I want to return the number of digits in the int -> values like this:

(int)1 => 1
(int)123 => 3
(int)12345678 => 8

I kno

6条回答
  •  忘掉有多难
    2020-12-31 21:38

    Use logarithms base 10:

    int length = (int)floor(log10((float)number)) + 1; // works for >0
    

提交回复
热议问题