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