Shop OBEX P1 Docs P2 Docs Learn Events
Get length of a LONG? — Parallax Forums

Get length of a LONG?

eagletalontimeagletalontim Posts: 1,399
edited 2014-06-14 23:56 in Propeller 1
If I have a LONG that totals 12345, how can I get the length of the LONG which would be 5? I am trying to clear the characters after the number is display on an LCD without clearing all the spaces. Sometimes the length will be 2 numbers, sometimes it will be 5 numbers.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-13 21:30
    Using Simple_Numbers.spin as num:
    length := strsize(num.dec(your_long))

    -Phil
  • AribaAriba Posts: 2,690
    edited 2014-06-14 15:07
    Or include a little subroutine like this:
    PRI DecSize(value) : n
      repeat
        n++
      until (value/=10) < 1
    
    (not tested)

    length := DecSize(myLong)

    Andy
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-06-14 18:12
    Another possibility, though not as trim or sexy as what's been suggested:
    pub digits(value)
    
      case value
        0..9       : return 1
        10..99     : return 2
        100..999   : return 3
        1000..9999 : return 4
        other      : return 5
    



    For numbers, I like to right-justify in displayed. I have this bit of code in my serial and LCD objects:
    pub rjdec(val, width, pchar) | tmpval, pad
    
    '' Print right-justified decimal value
    '' -- val is value to print
    '' -- width is width of (padded) field for value
    '' -- pchar is [leading] pad character (usually "0" or " ")
    
    '  Original code by Dave Hein
    '  Added (with modifications) to FDS by Jon McPhalen
    
      if (val => 0)                                                                 ' if positive
        tmpval := val                                                               '  copy value
        pad := width - 1                                                            '  make room for 1 digit
      else
        if (val == negx)                                                            '  if max negative
          tmpval := posx                                                            '    use max positive for width
        else                                                                        '  else
          tmpval := -val                                                            '    make positive
        pad := width - 2                                                            '  make room for sign and 1 digit
       
      repeat while (tmpval => 10)                                                   ' adjust pad for value width > 1
        pad--
        tmpval /= 10
         
      repeat pad                                                                    ' print pad
        tx(pchar)
    
      dec(val)                                                                      ' print value
    
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2014-06-14 19:14
    Too bad there isn't a ... LOG(10) function
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-14 21:26
    Too bad there isn't a ... LOG(10) function
    It could be done that way:
    log10(x) = log2(x) / log2(10)

    But after the ROM table lookups, interpolations, and division, I wouldn't trust it to be accurate near the power-of-ten boundaries.

    -Phil
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2014-06-14 23:56
    If I have a LONG that totals 12345, how can I get the length of the LONG which would be 5? I am trying to clear the characters after the number is display on an LCD without clearing all the spaces. Sometimes the length will be 2 numbers, sometimes it will be 5 numbers.

    Sometimes those digits mean something such as 000 where it would be important to know that 3 digits were entered. If this number being displayed is a keypad entry then simply count each digit as it is entered. The other thing is that I am a bit worried about is that you are so worried about clearing the number on the LCD in such a precise manner, what is the problem with clearing 5 digits all the time?
    The problem of course with all out answers is that the question in couched in such a narrow context which can only mean a narrow answer, which might just miss the mark.
Sign In or Register to comment.