microcontrolling/lib/util.c
2023-09-08 10:46:07 +02:00

33 lines
572 B
C

int num_digits(int num) {
if (num < 0) num = -num;
if (num >= 10) return num_digits(num / 10) + 1;
return 1;
}
// chatgpt generated
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;
}