microcontrolling/lib/util.c

32 lines
551 B
C
Raw Normal View History

2023-08-27 19:01:23 +00:00
int num_digits(int num) {
if (num < 0) num = -num;
if (num >= 10) return num_digits(num / 10) + 1;
return 1;
}
int get_digit_at_position(int number, int index) {
if (index < 0) {
return -1;
}
int divisor = 1;
int tempNumber = number;
while (tempNumber >= 10) {
tempNumber /= 10;
divisor *= 10;
}
if (index >= divisor) {
return -1;
}
for (int i = 0; i < index; i++) {
number /= 10;
}
int digit = number % 10;
return digit;
}