Shop OBEX P1 Docs P2 Docs Learn Events
String manipulation question — Parallax Forums

String manipulation question

SeariderSearider Posts: 290
edited 2008-07-03 12:16 in Propeller 1
I am trying to format the printout of a number to include a fixed Decimal point.· I am using integer math and have scaled the number so it is 100 X the actual number and thus the two least significant digits should be behind a decimal place.· Example: The value in memory is 1268. I would like to display this as 12.68.· I have looked through the object exchange but have not found any basic string manipulation functions that would let me· insert the decimal point into the string. I am sure others have done this many times. I realize that it would not be too difficult to write the code to do the buffer manipulations but I would like to hear from people before I code a basic building block type of routine. Any suggestions on good approaches would be greatly appreciated.
·
Thanks

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Searider

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-03 03:52
    It's much easier to insert the decimal point character "on the fly". Here's the decimal output routine from FullDuplexSerial:
    PUB dec(value) | i
    
    '' Print a decimal number
    
      if value < 0
        -value
        tx("-")
    
      i := 1_000_000_000
    
      repeat 10
        if value => i
          tx(value / i + "0")
          value //= i
          result~~
        elseif result or i == 1
          tx("0")
        i /= 10
    
    

    Modified to have a fixed decimal point:
    PUB decFixed(value,places) | i, power
    
    '' Print a decimal number
    
      if value < 0
        -value
        tx("-")
    
      i := 1_000_000_000
    
      repeat power from 10 to 1  ' Keep track of power of 10
        if power == places            ' If equal to # places wanted
          tx(".")                            '   then put out a decimal point
          result~~                        '     and force zeros to be output
        if value => i
          tx(value / i + "0")
          value //= i
          result~~
        elseif result or i == 1
          tx("0")
        i /= 10
    
    
  • SeariderSearider Posts: 290
    edited 2008-07-03 12:16
    Thanks Mike.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    Searider
Sign In or Register to comment.