We recommend switching to the latest versions of Edge, Firefox, Chrome or Safari. Using Internet Explorer will result in a loss of website functionality.

Formatting Numbers when combining fields

Comments

6 comments

  • Avatar
    Geoff

    well I found a solution, although there may be a cleaner one?

     

    val=field(Fields[i].str())
    if field(Fields[i].str()).isNumber() then val=format("%.2f",double(field(Fields[i].str())))
    msg = msg + "<td width=150 align=right>" + val + "</td>"

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Maybe something like this (which just creates a pipe-delimited list of the fields):

     

     

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    #### as text:

    #### ConfigureFields script

    #Configure all fields from input 'in1' to be mapped
    #to the corresponding fields on the output 'out1'
    out1 += in1

    out1.messageString = unicode

    ## Get the field names as a list
    FieldList = fields.todict().keys()

    ## Construct a dictionary with the field types
    fieldTypes = {}
    for fieldName in FieldList:
      fieldTypes.update({fieldName:fields[fieldName].type()})

    ## Calculate the number of input fields
    numFields = len(FieldList)

     

    #### ProcessRecords Script

    out1 += in1

    ## Use the first value to initialize the msg string
    ## (assumes it is not a float in this example)
    msg = str(FieldList[0])

    ## For the remaining fields
    i = 1
    while i < numFields:
      if fieldTypes[FieldList[i]] == float:
        ## Format the floating point number
        val = "{:.2f}".format(fields[FieldList[i]])
      else:
        ## Just turn it into a string
        val = str(fields[FieldList[i]])
      ## Construct a pipe delimited list of the fields
      msg = msg + " | " + val
      i += 1

    out1.messageString = str(msg)

     

    0
    Comment actions Permalink
  • Avatar
    Geoff

    ahhh

     

    if fieldTypes[FieldList[i]] == float:
        ## Format the floating point number
        val = "{:.2f}".format(fields[FieldList[i]])

     

    Nice!! thats perfect. Thank you!

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    You are welcome. I forgot to mention handling of Null values in the fields. You can do this by changing the ProcessRecords script to include a handler (in this case output "NULL"):

    out1 += in1

    ## Use the first value to initialize the msg string
    ## (assumes it is not a float in this example)
    msg = str(fields[FieldList[0]])

    ## For the remaining fields
    i = 1
    while i < numFields:
      if fieldTypes[FieldList[i]] == float:
        ## Format the floating point number
        if fields[FieldList[i]] is Null:
          val = "NULL"
        else:
          val = "{:.2f}".format(fields[FieldList[i]])
      else:
        if fields[FieldList[i]] is Null:
          val = "NULL"
        else:
          ## Just turn it into a string
          val = str(fields[FieldList[i]])
      ## Construct a pipe delimited list of the fields
      msg = msg + " | " + val
      i += 1

    out1.messageString = str(msg)

     

     

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    I also had an error in the initialization of the msg variable: it is now the value of the field rather than the field name 

    0
    Comment actions Permalink

Please sign in to leave a comment.