Finding the number of digits of an integer

前端 未结 17 2073
难免孤独
难免孤独 2020-12-07 16:32

What is the best method to find the number of digits of a positive integer?

I have found this 3 basic methods:

  • conversion to string

             
    
    
            
17条回答
  •  伪装坚强ぢ
    2020-12-07 16:54

    There's always this method:

    n = 1;
    if ( i >= 100000000 ) { n += 8; i /= 100000000; }
    if ( i >= 10000     ) { n += 4; i /= 10000; }
    if ( i >= 100       ) { n += 2; i /= 100; }
    if ( i >= 10        ) { n += 1; }
    

提交回复
热议问题