Programming tomb!!!

I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working. :frowning:

I need an object that receives two parameters:

  1. a number (0 to 99999 "long type")
  2. a place number (0 to 5 "int type")
    with these parameters it returns the digit of the number determine by the place number.

example 1:

  1. number: 56789
  2. place number: 1
    it returns 9

example 2:

  1. number: 56789
  2. place number: 2
    it returns 8

I believe that I made my point, I added my code below. Nevertheless I can't get it to work without overflowing. Any suggestions will be appreciated.

Thanks in advance.

int getDigit(long number,int place){
  if(place == 1){return (number-((int(number*0.1))*10));}
  else if(place == 2){return (int((number-((int(number*0.01))*100))*0.1));}
  else if(place == 3){return (int((number-((int(number*0.001))*1000))*0.01));}
  else if(place == 4){return (int((number-(long(int(number*0.0001)))*10000)*0.001));}
  else{return (int(number*0.0001));}
}

How about:

#include <math.h>

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}

So I used your code like such:

#include <math.h>
boolean boo = true;
void setup(){
  Serial.begin(9600);
}
void loop(){
  while(boo){
    delay(4000);  
    Serial.println(getDigit(56789L,1));
    Serial.println(getDigit(56789L,2));
    Serial.println(getDigit(56789L,3));
    Serial.println(getDigit(56789L,4));
    Serial.println(getDigit(56789L,5));
    boo = false;
    }
}

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}

and got as result:
9
8
3
6
5

Your code is ingenious but it stills has problems. Thanks

I don't have my Arduino here so I can't test, but it works fine on Linux using int as long and short as int. Must be an Arduino integer or math library issue.

You will need to split the functionality up and see at what point it is failing:

int getDigit(long number, int place)
{
    double p = pow(10, place - 1);
    long l = long(p);
    long n = number / l;
    long m = n % 10;

    // Debug output
    Serial.print("getDigit() called, number = ");
    Serial.print(number);
    Serial.print(", place = ");
    Serial.print(place);
    Serial.print(", l = ");
    Serial.print(l);
    Serial.print(", n = ");
    Serial.print(n);
    Serial.print(", m = ");
    Serial.println(m);

    return int(m);
}

pow() returns a float. Arduino is MUCH happier with integers:

int getDigit(long number, int place)
{
   for (; place>1; place--)
      number /= 10;   // Shift right one decimal digit
 
   return number % 10;  // Return rightmost decimal digit
}

[/quote]

ejahable:
I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working. :frowning:

6 ft surely?

Try converting to a string:

int getDigit (long number, int place)
 {
 char buf [12];
 ltoa (number, buf, 10); 
 // sanity clause
 if (place <= 0 || place > strlen (buf))
   return 0;
 // extract digit
 return (buf [strlen (buf) - place] - '0');
 }  // end of getDigit
 
void setup ()
{
  Serial.begin (115200);
  
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (56789L, i));

  Serial.println ();
  
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (2147483648L, i));
  
}  // end of setup

void loop ()
{}

Example output:

9
8
7
6
5
0
0
0
0
0

8
4
6
3
8
4
7
4
1
2

pow() returns a float. Arduino is MUCH happier with integers

Agreed, but I set myself the goal of trying to do it in one line (not counting the #include).

This feels suspiciously like a homework assignment, but I'll bite:

unsigned int homework(unsigned long x,unsigned int m)
{
  while (--m)
    x/=10;
  return x%10;
}

Will work for 1-max digits in number, unsigned only; no guarantees outside of that...

... just noticed johnwasser already wrote code that works, so didn't need to include mine - however, it's already done, it's another variation, so I'll leave it be (sorry john - should have checked more carefully).

Thanks all of you, I really appreciated!! and no its not for school. :stuck_out_tongue_closed_eyes:
lol, you all guys really know this stuff. Kudos!! 8)