多种方法求java求整数的位数

方法一
private static int getNumLenght(long num){
        num = num>0?num:-num;       
        return String.valueOf(num).length();

    }

方法二
private static int getNumLenght(long num){
        num = num>0?num:-num;       
        if (num==0) {
            return 1;
        }
        return (int) Math.log10(num)+1;
    }

方法三
private static int getNumLenght(long num){
        if (num==0) {
            return 1;
        }
        int lenght = 0;
        for (long temp = num ; temp != 0; temp/=10){
            lenght++;
        }
        return lenght;
    }

  

posted @ 2017-12-21 18:16  toov5  阅读(6121)  评论(0编辑  收藏  举报