有没有比这个方法更简洁的方法来获取整数的位数?

int numDigits = String.valueOf(1000).length();

当前回答

那简单的数学呢?除以10,直到0。

public static int getSize(long number) {
        int count = 0;
        while (number > 0) {
            count += 1;
            number = (number / 10);
        }
        return count;
    }

其他回答

这取决于你对“整洁”的定义。我认为下面的代码相当简洁,运行速度也很快。

它基于Marian的回答,扩展到所有long值,并使用?:运营商。

private static long[] DIGITS = { 1l,
                                 10l,
                                 100l,
                                 1000l,
                                 10000l,
                                 100000l,
                                 1000000l,
                                 10000000l,
                                 100000000l,
                                 1000000000l,
                                 10000000000l,
                                 100000000000l,
                                 1000000000000l,
                                 10000000000000l,
                                 100000000000000l,
                                 1000000000000000l,
                                 10000000000000000l,
                                 100000000000000000l,
                                 1000000000000000000l };

public static int numberOfDigits(final long n)
{
    return n == Long.MIN_VALUE ? 19 : n < 0l ? numberOfDigits(-n) :
            n < DIGITS[8] ? // 1-8
              n < DIGITS[4] ? // 1-4
                n < DIGITS[2] ? // 1-2
                  n < DIGITS[1] ? 1 : 2 : // 1-2
                        n < DIGITS[3] ? 3 : 4 : // 3-4
                      n < DIGITS[6] ? // 5-8
                        n < DIGITS[5] ? 5 : 6 : // 5-6
                      n < DIGITS[7] ? 7 : 8 : // 7-8
            n < DIGITS[16] ? // 9-16
              n < DIGITS[12] ? // 9-12
                n < DIGITS[10] ? // 9-10
                  n < DIGITS[9] ? 9 : 10 : // 9-10
                        n < DIGITS[11] ? 11 : 12 : // 11-12
                      n < DIGITS[14] ? // 13-16
                        n < DIGITS[13] ? 13 : 14 : // 13-14
                      n < DIGITS[15] ? 15 : 16 : // 15-16
            n < DIGITS[17] ? 17 :  // 17-19
            n < DIGITS[18] ? 18 :
            19;
}

对数是你的朋友:

int n = 1000;
int length = (int)(Math.log10(n)+1);

NB:只对n >有效0。

玛丽安的解决方案,现在是三元:

 public int len(int n){
        return (n<100000)?((n<100)?((n<10)?1:2):(n<1000)?3:((n<10000)?4:5)):((n<10000000)?((n<1000000)?6:7):((n<100000000)?8:((n<1000000000)?9:10)));
    }

因为我们可以。

下面是JDK开发人员给出的解决方案。JDK 17 (Long类):

/**
 * Returns the string representation size for a given long value.
 *
 * @param x long value
 * @return string size
 *
 * @implNote There are other ways to compute this: e.g. binary search,
 * but values are biased heavily towards zero, and therefore linear search
 * wins. The iteration results are also routinely inlined in the generated
 * code after loop unrolling.
 */
static int stringSize(long x) {
    int d = 1;
    if (x >= 0) {
        d = 0;
        x = -x;
    }
    long p = -10;
    for (int i = 1; i < 19; i++) {
        if (x > p)
            return i + d;
        p = 10 * p;
    }
    return 19 + d;
}

注意,如果需要的话,该方法会考虑减号。

不幸的是,该方法没有公开。

在性能方面,您可以从评论中看到,JDK开发人员与其他选项相比至少考虑了这一点。我猜 分而治之的方法倾向于较小的数字,效果会稍好一些 更好,因为CPU可以比整数更快地进行整数比较 乘法。但这种差异可能小到无法测量。

无论如何,我希望JDK中已经公开了这个方法,这样人们就不会开始使用自己的方法了。

理想情况下,一个整数除以10的倍数将返回位数,只要该整数不为零。这样一个简单的方法可以创建如下所示。

public static int getNumberOfDigits(int number) {
    int numberOfDigits = 0;                
    while(number != 0) {
        number /= 10;
        numberOfDigits++;
    }
    
    return numberOfDigits;
}