blob: 76b373af03f091962204af63fa7d00a8158dff29 [file] [log] [blame]
Borislav Petkova17bce42013-09-30 11:56:24 +02001/*
2 * Count the digits of @val including a possible sign.
3 *
4 * (Typed on and submitted from hpa's mobile phone.)
5 */
Borislav Petkov646e29a2013-09-27 16:35:54 +02006int num_digits(int val)
7{
Borislav Petkova17bce42013-09-30 11:56:24 +02008 int m = 10;
9 int d = 1;
Borislav Petkov646e29a2013-09-27 16:35:54 +020010
Borislav Petkova17bce42013-09-30 11:56:24 +020011 if (val < 0) {
12 d++;
13 val = -val;
Borislav Petkov646e29a2013-09-27 16:35:54 +020014 }
15
Borislav Petkova17bce42013-09-30 11:56:24 +020016 while (val >= m) {
17 m *= 10;
18 d++;
19 }
20 return d;
Borislav Petkov646e29a2013-09-27 16:35:54 +020021}