# Getting the nth digit of a number

Say, the number is "15" and i want to get the value of the 2nd digit which is "5". and store that 5 in an int variable.

another one is.. say, the number is "155" and i want to separate the first two digits and the 3rd digit. so it should store "15" in an int variable, and "5" also in another int variable..

been reading from different sources but none worked..

codes i tried(from different website):

``````number = 123456789
n = 5

tmp1 = (int)(number / 10^n);   // tmp1 = 12345
tmp2 = ((int)(tmp1/10))*10;    // tmp2 = 12340
digit = tmp1 - tmp2;           // digit = 5
``````

also:

``````int second = (number / 10) % 10;
``````

`(number / 10^n);`Ask yourself "Why do I have an XOR operation in that expression?"

somethingterrible:
Say, the number is “15” and i want to get the value of the 2nd digit which is “5”. and store that 5 in an int variable.

Depends on if you mean “second digit from the left” or “low-order digit”. The first question is hard. The second is easy: number%10.

somethingterrible:
another one is… say, the number is “155” and i want to separate the first two digits and the 3rd digit. so it should store “15” in an int variable, and “5” also in another int variable…

Again: mAJOR difference between “first two digits” and “high-order digits”. To separate the nth digit and the digits to the left of it:

``````long number = 123456789;
int n = 5;
long temp = number;
int nthDigit=temp % 10;
for (int i=0; i<n; i++) {
nthDigit=temp % 10;
temp /= 10;
}
long leftPart = temp;
``````

GrooveFlotilla:

``````(number / 10^n);
``````

Ask yourself "Why do I have an XOR operation in that expression?"

Hi.. Thanks!

``````tmp1 = (int)(number / pow(10, n));
``````

still doesnt work though..

johnwasser:
Depends on if you mean “second digit from the left” or “low-order digit”. The first question is hard. The second is easy: number%10.

Again: mAJOR difference between “first two digits” and “high-order digits”. To separate the nth digit and the digits to the left of it:

``````long number = 123456789;
``````

int n = 5;
long temp = number;
int nthDigit=temp % 10;
for (int i=0; i<n; i++) {
nthDigit=temp % 10;
temp /= 10;
}
long leftPart = temp;

hi… thanks for your reply… your first answer is what i need right now as i only need the least significant digit of 2-digit number.

i will explore your 2nd answer… I limit myself to 3 digit input though. so will try to shorten the code…

thanks everyone!

somethingterrible:

``````tmp1 = (int)(number / pow(10, n));
``````

still doesnt work though..

pow() returns a float.
'number' has more digits than can fit in a float.
'number' has more digits than can fit in an 'int' so for values or 32767, store it in a 'long'.

A completely other way is using my PrintCharArray class

``````//    FILE: PrintCharArrayDemo.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo experimental

#include "PrintCharArray.h"

PrintCharArray ps;

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

uint32_t number = 987654321;
Serial.println(number);

ps.println(number); // printe the number to a charArray

for (int i = 0; i < ps.size(); i++)
{
char digit = ps.getBuffer()[i];
Serial.println(digit);
}

Serial.println("done...");
}

void loop() {}

// END OF FILE
``````
``````int n = 12345;  // or whatever number

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

@somethingterrible

What should happen if the long number does not have an Nth digit?

e.g. long = 123 and you want the fourth digit?

should it return a 0, or -1 to indicate an error?
How robust should the solution be?