Split decimal digits


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2006
    Posts
    31

    Question Split decimal digits

    Anybody who has an idea on how to split the value of a 4-digit word variable into ones, tenths, hundreths and thousands.

    Example:

    The program is performing counting from 0 to 9999.
    Counter Var Word

    Assuming Counter has a value of 5389.
    I'm planning to store 9 to Digit1s variaible
    8 to Digit10s variable
    3 to Digit100s variable
    5 to Digit1000s variable
    I tried using DIG operator, only the first two decimal digits are correct (ones & tenths).

    Is there any mathematical method of splitting decimal digits?

  2. #2
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default DIG works for me at least

    Hi,

    It works for me. Can you post more of your code I always use it for my Seven Segment Display Routines. http://www.melabs.com/resources/pbpmanual/4_0.htm#4177
    Regards

    Sougata

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The DIG operator should work for that, I can't say why it doesn't work for you. A mathematical way may be:
    Code:
    Counter var word
    Thousands var byte
    Hundreds var byte
    Tens var byte
    Ones var byte
    
    Counter = 5389
    
    Thousands = Counter / 1000                       'Thousands is now 5
    Counter = Counter - (Thousands * 1000)      'Counter is now 389
    
    Hundreds = Counter / 100                           'Hundreds is now 3
    Counter = Counter - (Hundreds * 100)          'Counter is now 89
    
    Tens = Counter / 10                                  'Tens is now 8
    
    Ones = Counter - (Tens * 10)                     'Ones is now 9
    /Henrik Olsson.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    DIG work, have a look at the following
    http://www.picbasic.co.uk/forum/showthread.php?t=1044
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jan 2006
    Posts
    31


    Did you find this post helpful? Yes | No

    Default emavil's reply

    thanks a lot guys, i think there's a minor glitch with the programming.
    It's working now with DIG.

    But the mathematical approach was good.

  6. #6
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    Dig is great function but how to find out how many digits in the number if you dont know it, and what Dig function will do if you try to asses position what do not exist ?
    W0 = 54321
    B1 = W0 DIG 6 <---?
    Last edited by therian; - 8th January 2007 at 10:51.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The manual says:
    DIG returns the value of a decimal digit. Simply tell it the digit number (0 - 4 with 0 being the rightmost digit) you would like the value of, and voila.
    This makes sense since the largest variable supported by PBP is 16bits so a word sizes variable can't be more than 5 digits.

    If you have a word sized variable and it's value is 23 and you ask for DIG 2 it will return the value 0 since your variable is 00023.

    HOWEVER, the program compiles just fine even if you say DIG 6. I don't know what it will return. Perhaps it "spills" over into the next byte in RAM.

    /Henrik Olsson.

  8. #8
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by therian, The Rian who asked the question
    Dig is great function but how to find out how many digits in the number if you dont know it, and what Dig function will do if you try to asses position what do not exist ?
    W0 = 54321
    B1 = W0 DIG 6 <---?

    What I get from the question is this:
    W0 is WORD

    Say W0 = 23.
    That makes it W0 = 00023.
    IF you want to know which digits are NOT zero then you need a "check" routine.

    Example: W1 = 00103. We have two zeros we do not need. So DIG3 and DIG4 are not required to be read (for some logical reason!)

    I think this is what the question was asking.
    In that case, starting from Left to Right, we have a check routine.
    Code:
    IF W1 DIG 4 = 0 THEN
     IF W1 DIG 3 = 0 THEN
    ...
    
    ELSE
    'Do something....
    ELSE
    'Do something....
    ....
    etc.
    Do I make dead comments again?

  9. #9
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    wait, does it mean PBP will make word always 5 digit and byte 3 digit? hm I newer seen it in my book
    Last edited by therian; - 9th January 2007 at 00:26.

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I don't really follow you here....
    A word is always 16bits (0-65535) so yes, 5 digits. Digits "not used" will return a 0 when used with the DIG operator. If you are using the DIG operator for display purposes and don't want to display the zeros you need to use a check routine like in Sayzer's example.

    /Henrik Olsson.

  11. #11
    Join Date
    Jan 2007
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    I just wanted to build couple 7 led displays so I was thinking about how to measure how many digits in the number to send each number to led display

Similar Threads

  1. DS1820 display with 7-seg 4 digits
    By chai98a in forum Code Examples
    Replies: 12
    Last Post: - 10th April 2008, 13:12
  2. Time display on 7-seg 4 digits
    By chai98a in forum Code Examples
    Replies: 12
    Last Post: - 5th March 2007, 07:24
  3. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  4. 16bit variable and degrees conversion
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd May 2005, 17:27
  5. Decimal to binary conversion
    By Demon in forum Code Examples
    Replies: 9
    Last Post: - 25th February 2005, 20:05

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts