```import java.util.Arrays;`

/**
* Counts number decimal digits in a 32 bit signed number.
* 0 => 1, 9 => 1, 99 => 2 ... 2,147,483,647 => 10
* @param x number whose digits you wish to count.
* Must lie in range 0 .. Integer.MAX_VALUE;
*
* @return number of digits in x, e.g. Integer.toString(x).length()
* will always be 1 to 10
* @author Babu Kalakrishnan kala@sankya.com
*/
static final int [] limits=
{
9,99,999 ,9999,99999 ,999999,9999999 ,99999999,999999999
};

public static int widthInDigits ( int x )
{
// Still works even though some numbers larger than our
// 999,999,999 top limit
int n = Arrays.binarySearch( limits , x );
if ( n < 0 )
{
// no precise match, have (-insertion point -1)
return -n;
}
else
{
// precise match
return n + 1;
}
}

/* or better still with an unravelled binary search like this:*/

/**
* Counts number decimal digits in a 32 bit signed number.
* 0 => 1, 9 => 1, 99 => 2, 2,147,483,647 => 10
* @param x number whose digits you wish to count.
* Must lie in range 0 .. Integer.MAX_VALUE;
*
* @return number of digits in x, e.g. Integer.toString(x).length()
* @author Marc Chappuis marnic@ludomedia.ch
*/
public static int widthInDigits ( int x )
{
// do an unravelled binary search
if ( x < 10000 )
{
if ( x < 100 )
{
if ( x < 10 ) return 1;
else return 2;
}
else
{
if ( x < 1000 ) return 3;
else return 4;
}
}
else
{
if ( x < 1000000 )
{
if ( x < 100000 ) return 5;
else return 6;
}
else
{
if ( x < 100000000 )
{
if ( x < 10000000 ) return 7;
else return 8;
}
else
{
if ( x < 1000000000 ) return 9;
else return 10;
}
}
}
}```