Help splitting a # down to single chars

I have a # from 0 to 120
I get this # from a GPS and then print it out to an OLED and 4x20 i2c LED
all of that works dandy.

BUT I want to use large #'s for the speed

I need to split each # into an array or something so that say 0 - 9 would be single digit and pass it on
so it will print large #'s and if it's more then 1 char then to pass both #'s and more than 3 it will pass all
3

but for now a few static #'s are what I am dealing with to make it easy on my brain to try to grasp
What I don't want to do is 120 if statements lol.

#define CurSpeed 0
//#define CurSpeed 65
//#define CurSpeed 120

if (CurSpeed < 10) {
printbigchar(CurSpeed, 0, 2);
}
if (CurSpeed >= 10) {
printbigchar(CurSpeed1stDigit, 0, 2);
printbigchar(CurSpeed2ndDigit, 4, 2);
}
if (CurSpeed >99) {
printbigchar(CurSpeed1stDigit, 0, 2);
printbigchar(CurSpeed2ndDigit, 4, 2);
printbigchar(CurSpeed3rdDigit, 8, 2);
}

lcd.print("MPH");}

say 0 - 9 would be
printbigchar(CurSpeed, 0, 2); // this one works lol

then 10 - 99 would be
printbigchar(6, 0, 2);
printbigchar(5, 4, 2);

or say 120 would be
printbigchar(1, 0, 2);
printbigchar(2, 4, 2);
printbigchar(0, 8, 2);

any help would be greatly appreciated

To split a number you use a loop which gives them in reverse order. Assuming you have 3 digits to process.

// clear the buffer
for (int i=0; i<MAX_DIGITS; i++)
  digit[i] = 0;

// get the digits
index = MAX_DIGITS - 1;
temp = numberToSplit;
while (temp != 0)
{
  if (index != 0)  // prevent array overflow
  {
    digit[index] = temp % 10;
    index --;
  }
  temp = temp / 10;
}

// then do whatever you need with the digits array
[code]

marco_c:
To split a number you use a loop which gives them in reverse order. Assuming you have 3 digits to process.

// clear the buffer

for (int i=0; i<MAX_DIGITS; i++)
  digit[i] = 0;

// get the digits
index = MAX_DIGITS - 1;
temp = numberToSplit;
while (temp != 0)
{
  if (index != 0)  // prevent array overflow
  {
    digit[index] = temp % 10;
    index --;
  }
  temp = temp / 10;
}

// then do whatever you need with the digits array

[code]

thanks for the reply.

I had starting going down that trail but it only kinda works lol

#define TheNum 173
int hundreds;
int tens;
int ones;

int val = TheNum;

hundreds = val % 10 / 10; //now 1
tens = val / 10; // tens now = 7
ones = val % 10; // ones now = 3
//strip starting 0?

lcd.print(hundreds + "");
lcd.print(tens);
lcd.print(ones);
}
I sometimes get a beginning 0 on the 10s and ones that I need to suppress but I'm getting closer :slight_smile:

Simple way is to use sprintf, it will suppress leading zero's and fix the number into a fixed width field if you want to maintain the positioning on the LCD screen. You can also access each character individually if needed for some other part of your code.

#define TheNum 173
 int val = TheNum;
 char buffer[4];
 sprintf(buffer, "%3d", val);

 //print each character individually
 for (byte i = 0; i < 3; i++) {
   lcd.print(buffer[i]);
 }

 //print the entire character array at once
 lcd.print(buffer);