Select to view content in your preferred language

Thousands Separator for Label Expression using Python

8688
17
Jump to solution
10-20-2016 11:52 AM
ChrisDonohue__GISP
MVP Alum

How does one use Python to format a number label so it shows the thousands separator?  For example, I have a value of 12345 that I want to display (from the Shape.Area field of a File Geodatabase feature class) on the map as a label as 12,345.

I looked at these discussions, but can't seem to get it to work.

What is label expression for formatting a number to have thousands separator and decimals? 

python - Add 'decimal-mark' thousands separators to a number - Stack Overflow 

I guess I could slice the value and insert commas, but I was hoping to use something easier.

I'm using ArcGIS 10.2.1 (so Python 2.7.5).  I'm a beginner in Python, so there is a good chance I am just missing something basic in trying to do this.

Chris Donohue, GISP

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Hmmm... this works for me (field type is Long):

def FindLabel ( [CLIENTNUM] ):
  return "{:,}".format(float([CLIENTNUM]))

View solution in original post

17 Replies
DanPatterson_Retired
MVP Emeritus

>>> "{:,}".format(1000000)
'1,000,000'

BrittneyWhite1
Esri Contributor

From Building label expressions: "Field values are automatically cast to text strings. Therefore, if you wish to use a numeric value in an arithmetic operation, or when making a comparison, you will need to cast it back to a numeric data type."

 

This could look something like:

"{:,}".format(float([Field_Name]) )

ChrisDonohue__GISP
MVP Alum

If I purely use a number in the format, it works correctly.  But now the catch, how do I substitute in SHAPE_Area so it labels based on that field that with comma separators instead of a hard-coded number?

Chris Donohue, GISP

0 Kudos
DanPatterson_Retired
MVP Emeritus

python parser.... then why are the fieldnames enclosed in square brackets instead of ! !  ?? Is this unique to labelling? Chris, I don't do much labelling, but did you pick the parser before building the expression or after?  and what's up with the one space indentation? does Arc* put that indentation in or is that you?  

0 Kudos
ChrisDonohue__GISP
MVP Alum

I was noticing that too.  It doesn't matter if the Parser in the Label Expression is changed to Python first or not, the field names come across with brackets when one double-clicks on the field you want to select it.  On top of that, if one changes the field name to be surrounded by exclamation marks instead of brackets(with Python still selected as the Parser) it errors out:

As for the one-space indentation, it is actually two spaces and that seems to be what ArcGIS defaults to.

Chris Donohue, GISP

0 Kudos
DanPatterson_Retired
MVP Emeritus

so even str(!SHAPE_area!) errors out?  I am just wondering if the output is supposed to be string.

In any event, I don't do labelling, but I would be a tad peeved if Verify says nothing about what was wrong and more importantly, if the python parser is defective.  Good luck

0 Kudos
DarrenWiens2
MVP Honored Contributor

Hmmm... this works for me (field type is Long):

def FindLabel ( [CLIENTNUM] ):
  return "{:,}".format(float([CLIENTNUM]))
DanPatterson_Retired
MVP Emeritus

was that with the python parser and selecting the fields from the list? or by keying everything in?  That is one of the things Chris needs to figure out ... is it the process that is causing the error or the parser?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Python parser, and I don't recall if I selected from the list or not, but it's correct. The label expression box doesn't use the normal !Field! notation, just to keep you on your toes.