Shop OBEX P1 Docs P2 Docs Learn Events
Display formatting help needed — Parallax Forums

Display formatting help needed

Don MDon M Posts: 1,654
edited 2011-06-21 12:53 in Propeller 1
Let's say I have a statement in my code:
 term.dec(variable)

For conversations sake lets say the variable happens to be 36348. On the display it reads 36348 but I would like the variable to read 3.6348. How do you go about formatting (?) the variable to include a decimal point and maintain the 4 decimal places?

Thanks in advance.

Don

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-06-21 11:50
    How about splitting it in 2 parts:

    digit := variable / 10000
    fraction := variable // 10000

    Then output:
    term.dec(digit)
    term.tx('.')
    term.dec(fraction)

    If I remember right there was a trick for the rest of a division. Directly after the division you can use an internal variable to access the rest, so there is no need for a second division. Maybe someone else can jump in and confirm or reject this?!
  • Don MDon M Posts: 1,654
    edited 2011-06-21 12:18
    Ok that somewhat works but there is something wrong in the fractions part. It counts up to .9999 ok but when it rolls over to 1.0000 it then counts 1.1, 1.2, 1.3, 1.4,.... instead of 1.0001, 1.0002,...
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-06-21 12:31
    Oh ... right ... you have to zero-extend the fractional part. Easy solution:

    Have some if statements, which output the right number of zeros before output of fraction.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-21 12:35
    Unfortunately for this usage, the .DEC method doesn't put in leading zeroes. It's easier to modify the .DEC method to put in a decimal point. Here's a typical .DEC routine from FullDuplexSerial:
    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
    
    Here's a modified version that puts out a minimum number of digits so you get the leading zeroes:
    PUB decPlus(value,digits) | i, x, d
    
    '' 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 d from 10 to 1                                                          'Loop for 10 digits
        result |= d =< digits                                                          'Force leading zeroes when needed
        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
    
    Note that this is intended to be added to FullDuplexSerial. If it's just put into your program, add "term." in front of any "tx(" and you may need to change the "tx(" to "out(" depending on what display driver you're using.

    Try
    term.dec(value/10000)
    term.out(".")
    decPlus(value//10000,4)
    
    or something similar.
  • Don MDon M Posts: 1,654
    edited 2011-06-21 12:38
    Thanks Mike. Is your modified code included already in some object or are you just presenting it here?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-21 12:43
    I know I've used this before in something, but it's probably not in a posted object. It's really a trivial change to .DEC. You can even just stuff a "." into the output by changing

    result |= d =< digits

    to
    if d =< digits
       result~~
       tx(".")
    
  • Don MDon M Posts: 1,654
    edited 2011-06-21 12:53
    Thanks Mike. The object I found and posted about previous to your last post works exactly the way I need it to. I'm good for now. Problem solved. :)
Sign In or Register to comment.