Shop OBEX P1 Docs P2 Docs Learn Events
Extracting single digits from a 4 digit decimal number — Parallax Forums

Extracting single digits from a 4 digit decimal number

Gerry ShandGerry Shand Posts: 45
edited 2013-03-25 06:06 in BASIC Stamp
Hi All:

I am looking for a piece of code that will allow me to extract a single digit from a 4 digit decimal number.

As an example, I have a a program that does some computations and comes up with the result 1068. The range is from 0000 to 9999. All results are in decimal so I am only dealing with 0-9 in each position - no special symbols.

I was wondering how do I get at the individual digits (1, 0, 6 and 8 in the above example) to manipulate them individually? Once the digits have been isolated, I can then sequence them into a serial LCD with the option of inserting characters between the numbers. So I can end up displaying a result like: 1.0$6&8

I have tried using an array but it's not working.

Any thoughts on this? Thanks in advance for your help.

Regards,

Gerry

Comments

  • ercoerco Posts: 20,256
    edited 2013-03-23 20:19
    One straightforward way is to use the integer math function to truncate by division...
    b1=1068 ' your number
    b2=b1/10*10 ' now 1060
    0nes=b1-b2 ' now 8
    b1=b1/10 ' now 106
    b2=b1/10*10 ' now 100
    Tens=b1-b2 ' now 6
    etc...
  • Gerry ShandGerry Shand Posts: 45
    edited 2013-03-23 21:11
    Success! Here is the final code. Works like a hot tamale:
    '
    'DEFINE VARIABLES
    '

    DisplayData VAR Word
    DD1 VAR Word
    DD2 VAR Word
    Ones VAR Nib
    Tens VAR Nib
    Hundreds VAR Nib
    Thousands VAR Nib

    Program snippet

    DD1=DisplayData
    DD2=DD1/10*10
    Ones=DD1-DD2
    DD1=DD1/10
    DD2=DD1/10*10
    Tens=DD1-DD2
    DD1=DD1/10
    DD2=DD1/10*10
    Hundreds=DD1-DD2
    DD1=DD1/10
    DD2=DD1/10*10
    Thousands=DD1-DD2

    This works very well and only uses two variables for the number crunching. The end results have to be stored in nibble sized variables (4 in total here).

    Many thanks. My wife is impressed I actually got this up and running in a weekend.

    Regards,

    Gerrry S
  • ercoerco Posts: 20,256
    edited 2013-03-23 21:44
    XLNT. Happy wife, happy life!

    Further, you could use a subroutine for the two equations to save a bit of space.
  • ercoerco Posts: 20,256
    edited 2013-03-24 08:29
    Gerry: For extra credit, check out the // operator, which only returns the remainder. For instance: 7//4=3. Useful here!
  • SapphireSapphire Posts: 496
    edited 2013-03-24 11:30
    The Digit operator (DIG) does this for you automatically, where "value" holds your decimal number:
    ones = value DIG 0
    tens = value DIG 1
    hundreds = value DIG 2
    thousands = value DIG 3
    

    If you don't need the intermediate results, you can put the DIG operator in your SEROUT command:
    SEROUT pin,baud,[DEC value DIG 3,".",DEC value DIG 2,"$",DEC value DIG 1,"&",DEC value DIG 0]
    
  • ercoerco Posts: 20,256
    edited 2013-03-24 21:17
    Sapphire wins, and thus we all win! Nice, I just learned something new. I DIG it.!
  • Gerry ShandGerry Shand Posts: 45
    edited 2013-03-25 06:06
    I am just updating my code now. By using Sapphire's technique, the memory usage is reduced from 41% to 37% for my application. Funny that all the digging I went through in my books that I missed the DIG command. So like erco, I learned something new today. Thanks again guys. This problem is solved.
Sign In or Register to comment.