countDigits method

int countDigits(
  1. int amount
)

Count the digits (base 10) of the provided integer.

Implementation

int countDigits(int amount) {
  if (0 == amount) {
    return 1;
  }
  // if (0 > amount) return countDigits(-1 * amount);
  assert(0 < amount); // we prefer above, but linter complains
  var count = 0;
  while (amount > 0) {
    amount = amount ~/ 10;
    count++;
  }
  return count;
}