Shop OBEX P1 Docs P2 Docs Learn Events
AiGeneric Insert Decimal Function? — Parallax Forums

AiGeneric Insert Decimal Function?

SciNemoSciNemo Posts: 91
edited 2009-09-28 16:19 in Propeller 1
I am trying to write a function that takes any number and inserts it into the video buffer that the AiGeneric television text driver creates.

The place I am stuck at is extracting the digits in the number to a string. Once I can do that it is a simple matter of iterating through each digit and setting the corresponding buffer position equal to the digit.

So what is a fast way to extract the decimal digits in a number, preferably in Spin?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Not the fish.
sites.google.com/site/bitwinproject/

Comments

  • LeonLeon Posts: 7,620
    edited 2009-09-28 00:09
    Divide the number repeatedly by 10; the remainders are the digits.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • localrogerlocalroger Posts: 3,452
    edited 2009-09-28 00:35
    Enjoy!· It's relatively simple, but you get the digits in reverse order by taking the remainder.· You need to work backward in a buffer and then print the number forward once it's formed.· Here the buffer is a temp defined as 4 longs, though it's treated as a byte buffer via the byte operator.· If you want to insert the string into the buffer instead of printing it at the cursor, just replace the str() command with a bytemove to the appropriate place.
    PUB fdec(value, len, dp) | p, fnumbuf[noparse][[/noparse]4]
    '' Print a formatted signed decimal number
    '' The original dec function inexplicably doesn't do fixed
    '' length, even though the hex and bin functions do, making
    '' it nearly worthless for practical use.  This version also
    '' inserts an optional decimal point.  A negative dp specifies
    '' dead zeroes, e.g. dp=-3 returns 000 instead of 0.
    ''
    '' First we prep the buffer with any leading zero and
    '' decimals that might be called for, then write the number
    '' down backward skipping over the decimal and skipping
    '' over any remaining zeroes for the minus sign.
      bytefill(@fnumbuf," ",14)
      byte[noparse][[/noparse]@fnumbuf+14] := 0
      
      p := @fnumbuf + 13
      if dp > 0
        repeat dp
          byte[noparse][[/noparse]p] := "0"
          p--
        byte[noparse][[/noparse]p] := "."
        p--
        byte[noparse][[/noparse]p] := "0"
        p--
      elseif dp < 0
        repeat -dp
          byte[noparse][[/noparse]p] := "0"
          p--
      else
        byte[noparse][[/noparse]p] := "0"
        p--
             
      rdec(value, @fnumbuf+13)
      str(@fnumbuf + 14 - len)  
          
    PRI rdec(value, atloc) | s
    {
      This is works like dec but writes the number to memory
      down from the starting atloc.  It will not write over
      a decimal point; the string should be pre-loaded with
      any necessary leading zeroes and decimals.  There are
      no controls so be sure the buffer is large enough to
      handle a signed, decimalled 9-digit number.
    }
      s := 0
      if value < 0
        -value
        s := "-"
        
      repeat while value <> 0
        'skip over decimal
        if byte[noparse][[/noparse]atloc] == "."
          atloc --
        byte[noparse][[/noparse]atloc] := value // 10 + "0"
        atloc --
        value /= 10
      if s
        'skip over any non space before writing sign
        repeat while byte[noparse][[/noparse]atloc] <> " "
          atloc --
        byte[noparse][[/noparse]atloc] := s  
    

    Post Edited (localroger) : 9/28/2009 12:41:23 AM GMT
  • SciNemoSciNemo Posts: 91
    edited 2009-09-28 16:19
    Thanks for the help, but I seem to have solved my own problem. I started wading through your code and realized I had little idea what it was doing. I spent some quality time with my TI-84 and TI-BASIC which resulted in this working code:

    PRI extractDigits(num, str) | i, power, ndig, digit
    
      bytefill(@str, 0, 2)
    
      power := 0
    
      repeat while num/toPower(10, power)>9
        power += 1
                  
      ndig := power+1
    
      repeat i from 0 to ndig-1   
        digit := num/toPower(10, power)
        data[i] := digit
        num -= toPower(10, power)*digit
        power -= 1
        
    PRI toPower(val, pwr) | i, num
    
      num := val
    
      if pwr==1
        return val
      if pwr==0
        return 1
      if pwr>1
        repeat i from 0 to pwr-2
          num *= val
    
        return num
    [/i]
    



    The digits end up in the data array. I am only using 1 or 2 digit numbers, so this works well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Not the fish.
    sites.google.com/site/bitwinproject/
Sign In or Register to comment.