Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Horrible digit counting Java class
class DigitCounter {
public static int getCountsOfDigits(int n)
{
if (n > 999999999)
return 10;
if (n > 99999999)
return 9;
if (n > 9999999)
return 8;
if (n > 999999)
return 7;
if (n > 99999)
return 6;
if (n > 9999)
return 5;
if (n > 999)
return 4;
if (n > 99)
return 3;
if (n > 9)
return 2;
if (n > -1)
return 1;
/* FIXME: negative values? */
return 0;
}
public static void main(String args[])
{
System.out.println(getCountsOfDigits(0));
System.out.println(getCountsOfDigits(1));
System.out.println(getCountsOfDigits(10));
System.out.println(getCountsOfDigits(100));
System.out.println(getCountsOfDigits(1000));
System.out.println(getCountsOfDigits(10000));
System.out.println(getCountsOfDigits(100000));
System.out.println(getCountsOfDigits(1000000));
System.out.println(getCountsOfDigits(10000000));
System.out.println(getCountsOfDigits(100000000));
System.out.println(getCountsOfDigits(1000000000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment