One way to do this:
public boolean checkLength(int id, int length) {
return 0 == (int)(id / Math.pow(10, length));
}
EDIT:
According to the @EliSadoff comment below, you can also do something like this:
public boolean checkLength(int id, int length) {
return Math.log10(id) < length;
}
Then you can simply call this function as follows:
checkLength(123456, 6);
source
share