I'm having trouble rounding a percentage number using a label expression in Python. Just displaying the label as shown below is fine.

def FindLabel ( [Deaths], [PercentageDead] ):
    if ([Deaths]) == "0":
        return [Deaths]
        return [Deaths] + "\n" + [PercentageDead] + "%"      

I tried implementing the example given on Arcgis online but as soon as I try to round off the extra digits in my percentage all of those features go blank on my map. No label is displayed whatsoever for those feature.

def FindLabel ( [Deaths], [PercentageDead] ):
    if ([Deaths]) == "0":
        return [Deaths]
        P = round(float([PercentageDead]), 5)
        return [Deaths] + "\n" + P + "%"      

I've tried converting to a float or an int, and than converting it back to a string but nothing seems to work. If I try to manipulate that field whatsoever the expression will return nothing.

I just want to round off the digits to 5, how would I do that? I'd be willing to switch to Arcade if it would yield the same result.

1 Answer 1


Your P variable is a number and you're attempting to use the + operator with a number and strings. You cannot do this. You can + two strings or two numbers, but not a string and a number.

There are a few alternatives that you could try. My preference is to use the string format() function to do variable interpolation. It will convert numbers to strings on the fly.

Eg: return "{}\n{}%".format([Deaths], P)

This also has the advantage that it will not fail, even if there is a NULL (None) value, or some other unexpected data type. It will even convert that to a string, so at least you can see where the problem might be.

UPDATE (to address your comment):

The behaviour you've described in your comment is consistent with being unable to convert the string to a float (eg, because it has non-numeric characters in it).

You could wrap this in a try: block to test it, something like:

    pDead = float([PercentageDead])
    return "{}\nFAILED to convert '{}' to float!!!".format([Deaths], [PercentageDead])

P = round(float(pDead), 5)
return "{}\n{}%".format([Deaths], P)
  • As it turns out the error might be coming from the P declaration. Wherever I put it the code will stop executing at that exact line. Putting it at the top results in all labels being blank. Any ideas?
    – freebird
    May 4, 2020 at 2:00
  • What field type is your PercentageDead field? Is there any chance it is a string type that includes a % symbol? The behaviour you've described is consistent with being unable to convert the string to a float because it has non-numeric characters in it. May 4, 2020 at 2:19
  • I had actually fiddled with the field type and forgot about it, setting it back to numeric makes it all work great! If I did set the field type back to percentage, how would I ignore the % character when rounding it?
    – freebird
    May 4, 2020 at 2:25
  • In a string you can replace instances of a substring "%" with an empty string, "", like: float( [PercentageDead].replace("%", "") ) May 4, 2020 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.