misc.c 330 B

123456789101112131415161718192021222324
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <asm/misc.h>
  3. /*
  4. * Count the digits of @val including a possible sign.
  5. *
  6. * (Typed on and submitted from hpa's mobile phone.)
  7. */
  8. int num_digits(int val)
  9. {
  10. long long m = 10;
  11. int d = 1;
  12. if (val < 0) {
  13. d++;
  14. val = -val;
  15. }
  16. while (val >= m) {
  17. m *= 10;
  18. d++;
  19. }
  20. return d;
  21. }