Shop OBEX P1 Docs P2 Docs Learn Events
The DIG operator i Spin — Parallax Forums

The DIG operator i Spin

MoskogMoskog Posts: 554
edited 2010-10-08 01:55 in Propeller 1
Hello there!

I'm diving into the Propeller world and hope to dive so deep that I can understand the Spin language as good as PBasic. Everything is real difficult right now but I begin to understand a few things here.
But I got this problem when trying to transform a PBasic subroutine into a PUB methode.

It is the DIG-operator in PBasic, like this:
Temp = number DIG (index)
In this part of a routine I pull out each digit one by one of a big number, like in a number 5678 the Temp will be 7 if index = 3.

How do I do this in Spin?

Comments

  • max72max72 Posts: 1,155
    edited 2010-10-07 09:53
    Welcome to the insanely fun world of Propeller!!
    If you need to extract the numbers to format it as a string you can use the readily available objects (numbers and simple_numbers).
    Moreover if you check the objects you'll find a lot of code to handel that.
    Anyway the solution is to combine / and // (the two division operators).
    If you take 5678, and want to extract the 3rd digit, you can do something like that:
    (5678/10)//10
    5678/10 gives 567 (you must set the right power of 10 to extract the right digit)
    567//10 gives 6 (this part applies in every case)

    Massimo
  • MoskogMoskog Posts: 554
    edited 2010-10-07 13:26
    max72

    Thank you so much, that was a very simple solution and it works perfect.

    Now I can shift eight digits one by one through a MAX7219 and right into an eight digit display without problems.
    This is really getting fun!
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-10-07 16:13
    Here's a method that mimics the DIG operator and works with signed values.
    pub dig(value, pos)
    
    '' returns digit from specified position in value
    
      if (pos => 0) and (pos =< 9)
        if (value == negx)                       
          if (pos == 0)
            value := 8
          else
            value := posx
    
        value := ||value                                            ' make positive                                        
        repeat pos
          value /= 10                                               ' discard undesired digits
        result := value // 10                                       ' extract target digit
    
      else
        result := -1
    
  • MoskogMoskog Posts: 554
    edited 2010-10-08 01:55
    JonnyMac wrote: »
    Here's a method that mimics the DIG operator and works with signed values..

    Nice, I will be keeping your code as signed values also are a part of our life. Thank you!
Sign In or Register to comment.