Misc-Java-Examples

How to find the number of digits in a number

Posted On
Posted By admin

This blog post demonstrates how to count the number of digits in a String.

 

Method 1 – Using division


private static int countDigits(int input){
int count = 1;

if(input > 10){
int quotient = 0;
do{
count ++;
quotient = input/10;
input = quotient;

}while(quotient > 9);
}
return count;
}

The code checks if the number is greater than 10. If so, it divides the number by 10 and checks the quotient. It continues doing this as long as the quotient is greater than 9.

Method 2 – Using Strings


private static int countDigits(int input){
String str = String.valueOf(input);
return str.length();

}

Here, the input number is converted to a String and then its length is used to find the number of digits.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment