LAE

 View Only
  • 1.  Formatting Numbers when combining fields

    Posted 12-03-2019 09:46

    I am trying to output a "summary" to an HTML email.. and discovered that you need to combine the fields and do one row or you get one email per... yeah oops my first attempt sent me about 30 emails lol.

    so im using code like this to "build" the email

     

    i = 0
    while i < n {
    if i < 2 then {
    msg = msg + "<td><b>" + field(Fields[i].str()) + "<b></td>"
    } else {
    msg = msg + "<td><b>" + field(Fields[i].str()).round(-2) + "<b></td>"
    }
    i = i + 1
    }

    which is working, its just the numbers are coming out like this

    4.61463e+06

    (the input is 4614625.29) so its in the right format on the way in.

    ive tried format and it complains (maybe I was doing it wrong)... also note, there are nulls

    so something like

    format("%.2f",ifNull(field(Fields[i].str()).round(-2),0))

    or better if it stays null...

     

    thanks!!



  • 2.  RE: Formatting Numbers when combining fields

    Posted 12-03-2019 12:17

    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>"



  • 3.  RE: Formatting Numbers when combining fields

    Employee
    Posted 12-04-2019 05:03

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

     

     



  • 4.  RE: Formatting Numbers when combining fields

    Employee
    Posted 12-04-2019 05:05

    #### 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)

     



  • 5.  RE: Formatting Numbers when combining fields

    Posted 12-04-2019 06:23

    ahhh

     

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

     

    Nice!! thats perfect. Thank you!



  • 6.  RE: Formatting Numbers when combining fields

    Employee
    Posted 12-04-2019 06:52

    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)

     

     



  • 7.  RE: Formatting Numbers when combining fields

    Employee
    Posted 12-04-2019 06:57

    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