6

How would I split part1 into 2-3 digits and put them into num1, num2, and num3?

int FMSTATION = 10250;

int update_display() {
  int fm_station = FMSTATION ;

  int part1;
  int part2;

  part1 = (FMSTATION / 100);
  part2 = (FMSTATION % 100);
  part2 = (part2/10);

  int num1 = ;
  int num2 = ;
  int num3 = ;
  int num4 = part2;

  matrix.writeDigitNum(0, num1, false);  //1st number on lcd
  matrix.writeDigitNum(1, num2, false);  //2nd number on lcd
  matrix.writeDigitNum(3, num3, true);   //3rd number on lcd and adds "."
  matrix.writeDigitNum(4, num4, false);  //4th number on lcd

  matrix.writeDisplay();
}

I have searched the web from top to bottom, but could not find the answer. In the end what I should get displayed on my LCD is 102.5.

2 Answers 2

9

% 10 returns the final digit of a number. Dividing by 10 shifts the number one digit to the right.

So if you have the number 10250 as an integer you can get at each number with:

10250 % 10 = 0
(10250 / 10) % 10 = 5
(10250 / 100) % 10 = 2
(10250 / 1000) % 10 = 0
(10250 / 10000) % 10 = 1

So your code could be written as:

num4 = (fm_station / 10) % 10;
num3 = (fm_station / 100) % 10;
num2 = (fm_station / 1000) % 10;
num1 = (fm_station / 10000) % 10;

No need to mess with "part" variables.

1
1

I think this will help you:

int FMSTATION = 10250;
int update_display() {
  int fm_station = FMSTATION ;

  int part1;
  int part2; 
  part1 = (FMSTATION / 100); //102
  part2 = (FMSTATION % 100); //50
  part2 = (part2/10); //5

  int num1 = part1/100; //1
  int num2 = (part1 % 100)/10; //0
  int num3 = part1%10; //2
  int num4 = part2; //5

  matrix.writeDigitNum(0, num1, false); //1st number on lcd
  matrix.writeDigitNum(1, num2, false); //2nd number on lcd
  matrix.writeDigitNum(3, num3, true); //3rd number on lcd and adds "."
  matrix.writeDigitNum(4, num4, false); //4th number on lcd

  matrix.writeDisplay();
}
3
  • For num1 display, I would also check if it is not 0 before displaying it and clear the digit if it is, so that 95.6 would not get displayed as 095.6.
    – jfpoilpret
    Aug 6, 2016 at 7:34
  • Ok, i will try it out Aug 6, 2016 at 19:20
  • Thanks this did work, also i will try and implement the digit clear Aug 6, 2016 at 19:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.