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 )
{
int n = Arrays.binarySearch( limits , x );
if ( n < 0 )
{
return -n;
}
else
{
return n + 1;
}
}
/**
* 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 )
{
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;
}
}
}
}