Select to view content in your preferred language

Label expression concatenate and round

2373
3
Jump to solution
11-25-2012 07:47 PM
SiSharp
New Contributor
Hello,

I am trying to format half of my label red where it exceeds a set limit.

However, when I try and round the Field2 (which is numeric with many decimals) the python code does not allow me to concatenate.

any suggestions?

def FindLabel ( [Field1], [Field2] 😞
  if [Field2] >= 1:
     return "# " + [Field1] + "<CLR red='255'><FNT size = '16'>" + ", Field 2 " + round(float([Field2], 2) + "</FNT></CLR>"
    else:
  return "# " + [Field1] + ", Field 2 " + round([Field2],2)

Would very much appreciate any help with this simple task.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Occasional Contributor III
Try this code.
The following code assumes that [Field1] and [Field2] are numeric and your decimal separator is "." (dot):
def FindLabel ( [Field1], [Field2] ):   if float([Field1]) >= 1:     return "# " + str([Field1]) + ", " +  "<CLR red='255'><FNT size = '16'>" + "Field 2 " + str(round(float( [Field2]) , 2)) + "</FNT></CLR>"   else:     return "# " + str([Field1])  + ", Field 2 " + str(round(float([Field2]) ,2))


The results are like in this picture:
[ATTACH=CONFIG]19511[/ATTACH]

View solution in original post

0 Kudos
3 Replies
MarcinGasior
Occasional Contributor III
Try this code.
The following code assumes that [Field1] and [Field2] are numeric and your decimal separator is "." (dot):
def FindLabel ( [Field1], [Field2] ):   if float([Field1]) >= 1:     return "# " + str([Field1]) + ", " +  "<CLR red='255'><FNT size = '16'>" + "Field 2 " + str(round(float( [Field2]) , 2)) + "</FNT></CLR>"   else:     return "# " + str([Field1])  + ", Field 2 " + str(round(float([Field2]) ,2))


The results are like in this picture:
[ATTACH=CONFIG]19511[/ATTACH]
0 Kudos
SiSharp
New Contributor
Hi Marcin,

That works exactly the way I wanted, thank you very much for your help.

Cheers,
Simon
0 Kudos
curtvprice
MVP Esteemed Contributor
Very nice, Marcin!

Here's how I would go at it, at the risk of being accused of "gilding the lily"...

(As usual, I'm pushing Python's excellent string formatting capabilities!)

Expression:

FindLabel([Field1],[Field2])


Code block:

def FindLabel (f1,f2):
    if f1 >= 1:
        lbl = "# {0}, <CLR red='255'><FNT size='16'> Field 2: {1:.2f}</FNT></CLR>"
    else:
        lbl = "# {0}, Field 2: {1:.2f}"
    return lbl.format(f1,f2)
0 Kudos