Split a number into 10s and units

Probably really simple but...

I need to get the individual number from a time so i can output a nibble to a BCD to 10 decoder. So, if the time was 12:30 i need to find the 10s of hrs, hrs, 10s of minutes and minutes, so 1, then 2, then 3, then 0.

Im getting the time from a ds1307, so i can get just the hrs and just the minutes, but cant split them up! any help gratefully recieved.

Dave

What's your own mental process when you think about a number, if you want the "tens" and "units"?

The number 43 is ten times four, plus three. The number 43 divided by ten is four with a remainder of three. One way to get the remainder of a division is with the modulo operator. http://arduino.cc/en/Reference/Modulo

Trying to nudge you to a solution, rather than just handing you the two lines of code it takes. :slight_smile:

yeah i appreciate it! i can see how to get the tens, all im doing is dividing it by ten so with 12 i'd get 1.2 and becasue its not a float it ignores the .2 giving me just the 1. However it was the units that had me stumped! i shall research the modulo operator now! Thankyou!

You can always subtract the tens, multiplied by ten, from the original number.

ok, so this seems to be working! am i right in thinking that the modulo does the division, but only returns the whole number and ignores the decimal?

This is the code i've got now!

  if (hour > 9) {
    hourNibble10 = hour%10;
    hourNibble = hour - (hourNibble10 * 10);

  }

  if (hour < 9 ) {
    hourNibble10 = 0;
    hourNibble = hour;

  }

  if (minute > 9) {
    minNibble10 = minute%10;
    minNibble = minute - (minNibble10 * 10);

  }

  if (minute < 9 ) {
    minNibble10 = 0;
    minNibble = minute;
  }

Nope, the / does the division and keeps the whole number (unless the variables are float type). The % (modulo) does the division and keeps the remainder.

It can be done a lot simpler:

{
  hourNibble10 = hour/10;
  hourNibble = hour%10;
  minNibble10 = minute/10;
  minNibble = minute%10;
}

As said, modulo keeps the remainder of the division, and divide keeps the whole number.

Some examples:

  9/10 = 0
  9%10 = 9

 10/10 = 1
 10%10 = 0

 11/10 = 1
 11%10 = 1

right ok i understand now! your right, i was over complicating things! Thankyou for clarifying!