Thousands Separator for Label Expression using Python

7714
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
17 Replies
DanPatterson_Retired
MVP Emeritus

good catch if it works from the list selection...

I would deem this an appropriate use of 'pythonesque', perhaps VBthon?  

ChrisDonohue__GISP
MVP Alum

Monty Python, in search of the holy code: 

"But you're VB!  What are you doing in the Python Parser?" 

Chris Donohue, GISP

ChrisDonohue__GISP
MVP Alum

"The label expression box doesn't use the normal !Field! notation, just to keep you on your toes."

Good catch.  The whole lack of !Field! notation despite being in the Python mode was making me wonder if my software was corrupted.  Maybe we could submit this to ArcGIS Ideas to standardize this so folks won't be confused?

Chris Donohue, GISP

0 Kudos
DanPatterson_Retired
MVP Emeritus

that is a bug... if it advertised as python, it should behave lie python, there should be non 'esque' in parsers there is enough of that in the various database forms

ChrisDonohue__GISP
MVP Alum

That worked!  Thanks Darren.

Chris Donohue, GISP

0 Kudos
ClintonDow1
Occasional Contributor II

Hi Chris,

The cast is necessary because the field's value is returned as an object (that is, the Python base class), since arbitrary data types are supported by the expression dialog. String.format() does not work with uncast objects because an object.__repr__ method returns a 'method-wrapper' class and not a datatype that string.format knows how to work with. Once you've cast the object, in this case invoking its __float__ method, it returns its value as a float data type which plays nice with string formatting. 

DanPatterson_Retired
MVP Emeritus

Clinton, this would be a nice inclusion in the help topics.  It may not matter for most, but if casting is needed because a python object is used, then this means something to some of us.  

This is going to be an issue when people start migrating to Pro and start using Pandas which makes judicious use of python's object... most people will think

                      ... if it looks like a number, it should act like a number...

Indicating in the help that casting to float or string (I wont get into python 2 vs 3 'strings') should be used explicitly would at least warn people.  A few simple examples would also help.  I know that you aren't the person to implement this in the help, but I have a strong suspicion that the doc's people aren't big on object casting and representation.

Thanks

ClintonDow1
Occasional Contributor II

Hi Dan, I agree. I had to do a bit of digging to figure that out so I don't think its common knowledge but I'll definitely recommend that we mention it in the docs.