devxlogo

Counting Digits in a Given Number

A typical mechanism of converting to a string, traversing and counting has a lot of processing to complete.

Instead, the code below shows a combination of methods in Math package to help us achieve the same.

*/public class DigitCount{   public static void main(String args[])   {      DigitCount digitCount = new DigitCount();      digitCount.proceed();   }      private void proceed()   {      int aNumber = 56554;      System.out.println("Digit count in " + aNumber + ": " + Math.round(Math.floor(Math.log10(aNumber)) + 1));   }}/*

Expected output:

[root@mypc]# java DigitCountDigit count in 56554: 5*/ 

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.