Shop OBEX P1 Docs P2 Docs Learn Events
constant lenght of decimal — Parallax Forums

constant lenght of decimal

avionikerenavionikeren Posts: 64
edited 2010-10-23 13:44 in Propeller 1
I`m new to propeller, so i`m sorry for a noob question, but I have searched through the forum and can`t find the answer, though I`m sure it`s there...

I`m sending a string using full duplex serial object, and it works fine, but I want the string to be constant lenght. In bs2 i just put DEC3 for 3 digits, but how to do it in spin?

example from my code:

serial.dec(compass.heading)

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-10-23 08:18
    This method will do what you want. You can add it to FullDuplexSerial or use it externally. If used externally, the method calls to tx and dec will have to have the object reference prepended (e.g. sio.tx(" ") ).
    PUB decn(value, digits) | mindigits, val
    
    '' Print a decimal number, right-justified in a field width given by ||digits.
    '' If digits < 0, pad with leading zeroes; otherwise, pad with leading blanks.
    
      mindigits := ((value < 0) & 1) + 1
      if (value <> $8000_0000)
        val := || value
        repeat while (val /= 10)
          mindigits++
      if (value < 0 and digits < 0)
        tx("-")
        value := ||value
      repeat (||digits - mindigits) #> 0
        if (digits < 0) 
          tx("0")
        else
          tx(" ")
      dec(value) 
    
    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-23 08:24
    Here's a routine you can use to display or output a decimal number with a fixed field width and (optionally) a decimal point followed by a fixed number of decimal places. This would be useful if you had values in cents, but wanted to display them as dollar values with two decimal places. Note that this calls out() and str(). You'll have to change those if you're using something different (like to tx() and str()).
    PUB decFixed(value,width,numPlaces) | i, j, p, f, a, b, c, d
    '' Print a decimal number of fixed width with decimal places
      numPlaces #>= 0
      numPlaces <#= 10
      p := @a
      f~
      if value < 0
        -value
        byte[p++] := "-"
      i := 1_000_000_000
      repeat j from 1 to 10 - numPlaces
        if value => i
          byte[p++] := value / i + "0"
          value //= i
          f~~
        elseif f or j == (10 - numPlaces)
          byte[p++] := "0"
        i /= 10
      if numPlaces
        byte[p++] := "."
        repeat numPlaces
          if value => i
            byte[p++] := value / i + "0"
            value //= i
          else
            byte[p++] := "0"
          i /= 10
      byte[p]~
      i := strsize(@a)
      if i < width
        repeat width - i
          out(" ")
      str(@a)
    
  • max72max72 Posts: 1,155
    edited 2010-10-23 12:45
    I usually use the "simple numbers" object to do that.

    http://obex.parallax.com/objects/182/

    This one is overkill and sometimes not as immediate
    http://obex.parallax.com/objects/23/

    Massimo
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-10-23 13:44
    I also like the "simple numbers" object. Although it's not as handy having yet another object in your program, it does eliminate the duplication of things like dec and hex in low-level driver routines where, frankly, they don't belong. For output drivers, out (or tx) and str are sufficient. (And if we had method pointers, str would not be needed, either.)

    -Phil
Sign In or Register to comment.