Way to get number of digits in an int?

2018-12-31 19:40发布

Is there a neater way for getting the length of an int as this?

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

标签: java int
27条回答
梦该遗忘
2楼-- · 2018-12-31 20:09

Since the number of digits in base 10 of an integer is just 1 + truncate(log10(number)), you can do:

public class Test {

    public static void main(String[] args) {

        final int number = 1234;
        final int digits = 1 + (int)Math.floor(Math.log10(number));

        System.out.println(digits);
    }
}

Edited because my last edit fixed the code example, but not the description.

查看更多
梦该遗忘
3楼-- · 2018-12-31 20:11

With design (based on problem). This is an alternate of divide-and-conquer. We'll first define an enum (considering it's only for an unsigned int).

public enum IntegerLength {
    One((byte)1,10),
    Two((byte)2,100),
    Three((byte)3,1000),
    Four((byte)4,10000),
    Five((byte)5,100000),
    Six((byte)6,1000000),
    Seven((byte)7,10000000),
    Eight((byte)8,100000000),
    Nine((byte)9,1000000000);

    byte length;
    int value;

    IntegerLength(byte len,int value) {
        this.length = len;
        this.value = value;
    }

    public byte getLenght() {
        return length;
    }

    public int getValue() {
        return value;
    }
}

Now we'll define a class that goes through the values of the enum and compare and return the appropriate length.

public class IntegerLenght {
    public static byte calculateIntLenght(int num) {    
        for(IntegerLength v : IntegerLength.values()) {
            if(num < v.getValue()){
                return v.getLenght();
            }
        }
        return 0;
    }
}

The run time of this solution is the same as the divide-and-conquer approach.

查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 20:12

We can achieve this using a recursive loop

    public static int digitCount(int numberInput, int i) {
        while (numberInput > 0) {
        i++;
        numberInput = numberInput / 10;
        digitCount(numberInput, i);
        }
        return i;
    }

    public static void printString() {
        int numberInput = 1234567;
        int digitCount = digitCount(numberInput, 0);

        System.out.println("Count of digit in ["+numberInput+"] is ["+digitCount+"]");
    }
查看更多
余生请多指教
5楼-- · 2018-12-31 20:12

Enter the number and create an Arraylist, and the while loop will record all the digits into the Arraylist. Then we can take out the size of array, which will be the length of the integer value you entered.

ArrayList<Integer> a=new ArrayList<>();

while(number > 0) 
{ 
    remainder = num % 10; 
    a.add(remainder);
    number = number / 10; 
} 

int m=a.size();
查看更多
低头抚发
6楼-- · 2018-12-31 20:13

A really simple solution:

public int numLength(int n) {
  for (int length = 1; n % Math.pow(10, length) != n; length++) {}
  return length;
}
查看更多
人间绝色
7楼-- · 2018-12-31 20:14

The logarithm is your friend:

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

NB: only valid for n > 0.

查看更多
登录 后发表回答