Shop OBEX P1 Docs P2 Docs Learn Events
Printing a fixed length decimal number — Parallax Forums

Printing a fixed length decimal number

John_BJohn_B Posts: 16
edited 2009-10-06 16:52 in Propeller 1
Hi,
Is there any routines that print a fixed length signed decimal number.
If so, does the routine take care of the '-' or are positive numbers prefixed with '+' ?

ie.
00000
00005
12345
-0023

Thanks,
Jon

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-06 14:37
    It's easy to modify the existing variable length decimal output routines found in most Prop output drivers to produce fixed length output. They usually include a minus sign for negative numbers, but no sign for positive numbers.

    Here's one:
    PUB dec(value) | i, x
    
    '' Print a decimal number
    
      x := value == NEGX                                                            'Check for max negative
      if value < 0
        value := ||(value+x)                                                        'If negative, make positive; adjust for max negative
        tx("-")                                                                     'and output sign
    
      i := 1_000_000_000                                                            'Initialize divisor
    
      repeat 10                                                                     'Loop for 10 digits
        if value => i                                                               
          tx(value / i + "0" + x*(i == 1))                                          'If non-zero digit, output digit; adjust for max negative
          value //= i                                                               'and digit from value
          result~~                                                                  'flag non-zero found
        elseif result or i == 1
          tx("0")                                                                   'If zero digit (or only digit) output it
        i /= 10                                                                     'Update divisor
    


    Instead of "i == 1" in the 3rd from the bottom line, put "i < 100000" to get 5 digits, "i < 1000" to get 3 digits, etc.

    Note: This will append the minus sign to a fixed length numeric value (like "-001" or "001"). You will need some additional logic to either add an extra digit position if positive or to put out a "+" if positive.

    Post Edited (Mike Green) : 10/6/2009 2:43:39 PM GMT
  • John_BJohn_B Posts: 16
    edited 2009-10-06 16:52
    Mike,

    It took a bit of time to realise what was happening, but I have now modified the routine so a variety of number styles can be printed from the calling routines.

    Many thanks

    Jon
Sign In or Register to comment.