How to format decimal places in labels

1750
3
Jump to solution
10-30-2018 06:39 AM
SerenaFraccascia
New Contributor II

I have a point feature class for which I want to show labels based on a numeric "Field". My labels have to be rounded to the second decimal place and I need to have them padded with zeros. I know that I can manually modify it through the Layer Properties, but I need to write a script for that.

I have tried to run the following code, also based on other posts, but I end up with no labels at all:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

layer = arcpy.mapping.ListLayers(mxd, "Layername")[0]

if layer.supports("LABELCLASSES"):
    for lblClass in layer.labelClasses:
        lblClass.className = "Field"
        lblClass.expression = "Field".format("%.2f")
        layer.showLabels = True

I wonder whether I have missed some steps. Thanks in advance for your help!

0 Kudos
1 Solution

Accepted Solutions
StephenM
Occasional Contributor II

I think the reason why no labels are displaying is because that expression isn't valid in VBScript, which is the default label expression parser in ArcMap. It looks like a Python expression, but that format statement isn't quite right. Using the Python parser that expression is valid, but it would set all the labels to say "Field". What you'll probably want to do is use the VBScript FormatNumber function.

Other considerations:

  • That first statement in the for loop (lblClass.className = "Field") sets all the label class names to be "Field"—not sure if that's what you want.
  • You might also want the layer.showLabels statement to be outside the for loop so that it isn't set on each iteration of the loop.

If you're looking to label specific label classes in the layer, you could do something like this:

if layer.supports("LABELCLASSES"):
   for lblClass in layer.labelClasses:
      if(lblClass.className == "LabelThisClass"):
         lblClass.expression = "FormatNumber([field],2)"
         lblClass.showClassLabels = True
   layer.showLabels = True

Otherwise, if all the features in the layer are labeled the same way you could do something like this:

if layer.supports("LABELCLASSES"):
   for lblClass in layer.labelClasses:
      lblClass.expression = "FormatNumber([field],2)"

   layer.showLabels = True

This code uses VBScript for the label expression since that is the default parser. An equivalent Python expression could be written using the format() method"format(float([field]),'.2f')". I don't think there's a way to change the label expression parser using ArcPy though, so you'll probably want to go with VBScript.

A potentially useful reference:

Building label expressions—Help | ArcGIS Desktop

View solution in original post

3 Replies
StephenM
Occasional Contributor II

I think the reason why no labels are displaying is because that expression isn't valid in VBScript, which is the default label expression parser in ArcMap. It looks like a Python expression, but that format statement isn't quite right. Using the Python parser that expression is valid, but it would set all the labels to say "Field". What you'll probably want to do is use the VBScript FormatNumber function.

Other considerations:

  • That first statement in the for loop (lblClass.className = "Field") sets all the label class names to be "Field"—not sure if that's what you want.
  • You might also want the layer.showLabels statement to be outside the for loop so that it isn't set on each iteration of the loop.

If you're looking to label specific label classes in the layer, you could do something like this:

if layer.supports("LABELCLASSES"):
   for lblClass in layer.labelClasses:
      if(lblClass.className == "LabelThisClass"):
         lblClass.expression = "FormatNumber([field],2)"
         lblClass.showClassLabels = True
   layer.showLabels = True

Otherwise, if all the features in the layer are labeled the same way you could do something like this:

if layer.supports("LABELCLASSES"):
   for lblClass in layer.labelClasses:
      lblClass.expression = "FormatNumber([field],2)"

   layer.showLabels = True

This code uses VBScript for the label expression since that is the default parser. An equivalent Python expression could be written using the format() method"format(float([field]),'.2f')". I don't think there's a way to change the label expression parser using ArcPy though, so you'll probably want to go with VBScript.

A potentially useful reference:

Building label expressions—Help | ArcGIS Desktop

SerenaFraccascia
New Contributor II

Thanks for your help! By the way, I have noticed that if I ran the code in ArcMap, it used the VBScript parser for the label expression, as you wrote. However, when I integrated it into a model, it appeared that through the model the parser was automatically set to Python. I have not found a proper explanation for that, though.

0 Kudos
StephenM
Occasional Contributor II

That's interesting. Maybe another tool in the model changed the parser to Python. I'm not sure if that's possible, but I tested the script on its own in Model Builder and it used the VBScript.

0 Kudos