countDigits method

int countDigits()

This function counts the number of digits in an integer. It takes an integer as input and returns the number of digits in the integer.

The function handles both positive and negative integers, as well as zero.

It uses the method of repeatedly dividing the number by 10 until it becomes 0,

which is generally more efficient than converting the number to a string and getting its length.

Implementation

int countDigits() {
  // // Count the number of digits in the absolute value of value
  // return value.abs().toString().length;

  // If the number is 0, return 1
  if (this == 0) {
    return 1;
  }

  // Take the absolute value of the number
  int number = abs();

  // Initialize a counter for the number of digits
  int count = 0;

  // Repeat until the number becomes 0
  while (number != 0) {
    // Divide the number by 10
    number ~/= 10;

    // Increment the counter
    count++;
  }

  // Return the count
  return count;
}