Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 1 | /* |
2 | * Count the digits of @val including a possible sign. | ||||
3 | * | ||||
4 | * (Typed on and submitted from hpa's mobile phone.) | ||||
5 | */ | ||||
Borislav Petkov | 646e29a | 2013-09-27 16:35:54 +0200 | [diff] [blame] | 6 | int num_digits(int val) |
7 | { | ||||
Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 8 | int m = 10; |
9 | int d = 1; | ||||
Borislav Petkov | 646e29a | 2013-09-27 16:35:54 +0200 | [diff] [blame] | 10 | |
Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 11 | if (val < 0) { |
12 | d++; | ||||
13 | val = -val; | ||||
Borislav Petkov | 646e29a | 2013-09-27 16:35:54 +0200 | [diff] [blame] | 14 | } |
15 | |||||
Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 16 | while (val >= m) { |
17 | m *= 10; | ||||
18 | d++; | ||||
19 | } | ||||
20 | return d; | ||||
Borislav Petkov | 646e29a | 2013-09-27 16:35:54 +0200 | [diff] [blame] | 21 | } |