Way to get number of digits in an int?

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

    Using Java

    int nDigits = Math.floor(Math.log10(Math.abs(the_integer))) + 1;
    

    use import java.lang.Math.*; in the beginning

    Using C

    int nDigits = floor(log10(abs(the_integer))) + 1;
    

    use inclue math.h in the beginning

提交回复
热议问题