Java 如何计算int值中的位数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30410753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:34:58  来源:igfitidea点击:

How to count the number of digits in an int value?

javaarrays

提问by 30iT

Given the following code.

鉴于以下代码。

 int[] eg = {21,20,1,12,2}
 public numbers(int[] eg)
 {
     for(int i=0; i < eg.length; i++)

I would expect eg.lengthto be 5, where eg[0]would = 21 eg[1]would = 20, eg[3]would = 1

我希望eg.length是 5,其中eg[0]会 = 21eg[1]会 = 20, eg[3]会 = 1

I would like to check each element of eg[i] to see if it is a 1 digit or 2 digit number I tried eg[0].lengthto no avail, but am I correct in assuming that is only for Array Lists?

我想检查 eg[i] 的每个元素,看看它是 1 位还是 2 位数字eg[0].length,但我尝试无济于事,但我假设这仅适用于数组列表是否正确?

回答by Maroun

eg[i]is of type int, which doesn't have the lengthproperty.

eg[i]是 type int,它没有length属性。

There are many ways of telling how many digits the number has:

有很多方法可以告诉数字有多少位数:

  • write your own method that accepts an intand keeps dividing by 10 until the number reaches 0.

  • using math properties:

    int length = (int) (Math.log10(number) + 1);

  • converting the number to String and using its methods

  • 编写您自己的方法,该方法接受 anint并不断除以 10,直到数字达到 0。

  • 使用数学属性:

    int length = (int) (Math.log10(number) + 1);

  • 将数字转换为字符串并使用其方法

回答by MChaker

Convert the number to a String and get the length.

将数字转换为字符串并获取长度。

int length = String.valueOf(eg[i]).length();

Or

或者

int length = Integer.valueOf(eg[i]).toString().length();


Use this mathematical equation (assuming that all the data in the array are strictly positive numbers).

使用这个数学方程(假设数组中的所有数据都是严格的正数)。

int length = (int)(Math.log10(eg[i])+1);

回答by Tagir Valeev

There's an algorithm used in JDK which is probably the fastest one:

JDK 中使用了一种算法,它可能是最快的算法:

final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                  99999999, 999999999, Integer.MAX_VALUE };

// Requires positive x
static int stringSize(int x) {
    for (int i=0; ; i++)
        if (x <= sizeTable[i])
            return i+1;
}

回答by nxhoaf

If you want to see what is the number length, MChaker's solution is the correct one. However, if you want to see if the number contains one digit or more than one digit, then you might consider the following solution:

如果你想看看数字长度是多少,MChaker 的解决方案是正确的。但是,如果您想查看数字是否包含一位或多于一位,那么您可以考虑以下解决方案:

public class NumberTest {
public static void main(String[] args) {
    int[] eg = {21,20,1,12,2, -3, -8, -20};
    System.out.println("Array Length: " + eg.length);

    for(int i=0; i < eg.length; i++) {
        System.out.println("#" + i + ": Value: " + eg[i] + " one digit number: " + isOneDigitNumber(eg[i]));
    }
}
static private boolean isOneDigitNumber(int number) {
    return (-10 < number && number < 10);
}

}

}

and here is the result of above code:

这是上面代码的结果:

Array Length: 8
#0: Value: 21 one digit number: false
#1: Value: 20 one digit number: false
#2: Value: 1 one digit number: true
#3: Value: 12 one digit number: false
#4: Value: 2 one digit number: true
#5: Value: -3 one digit number: true
#6: Value: -8 one digit number: true
#7: Value: -20 one digit number: false
#7: Value: -20 one digit number: false

回答by Yves Daoust

To tell a one digit from a two digits number, compare to 10.

要从两位数中区分一位数,请与 10 进行比较。