I am currently working with a 4x 7-segment LED array and I’m trying to display numbers on it.

For this I need to code a function which returns the n-th digit of a number. I want my function to return 0 when the number is smaller than the requested digit. On another post (https://forum.arduino.cc/index.php?topic=524202.0) I found this “formula”:

```
int ones = n % 10;
int tens = (n / 10) % 10;
int hund = (n / 100) % 10;
int thou = (n / 1000) % 10;
int myri = (n / 10000) % 10;
```

I put this into a simple function:

```
int nthdigit(const int number, const int digit) {
return ((int) trunc(number / ((int) pow(10, digit)))) % 10;
}
```

And with `#include <cmath>`

on a normal x64 PC with gcc this works as expected.

**However if I try this on my ATmega168** with `math.h`

instead of `cmath`

I get weird results. Look at this example:

```
const int number = 5678;
const int digit = 2;
Serial.println((number / 100) % 10); // results in the expected 6
Serial.println(((int) trunc(number / ((int) pow(10, digit)))) % 10); // also results in the expected 6
Serial.println(nthdigit(number, 2)); // this one gives me a 7
```

That’s right - if I call the function that does literally the very same thing I did a line above, I get a different result. What could be causing this and how’d I fix it?

Thanks for any help in advance - this is my entire file:

```
#include <math.h>
int nthdigit(const int number, const int digit) {
return ((int) trunc(number / ((int) pow(10, digit)))) % 10;
}
void setup() {
const int number = 5678;
const int digit = 2;
Serial.begin(9600);
Serial.println("-----------");
Serial.println((number / 100) % 10);
Serial.println(((int) trunc(number / ((int) pow(10, digit)))) % 10);
Serial.println(nthdigit(number, 2));
}
void loop() {}
```